How to Use PHP In_Array with Associative Array

How to use PHP in_array with associative array?

Try this.....
You can use this function for any depth of the associated array. Just contraint to this function is that the key value would not be repeat any where in array.

<?php 
function is_in_array($array, $key, $key_value){
$within_array = 'no';
foreach( $array as $k=>$v ){
if( is_array($v) ){
$within_array = is_in_array($v, $key, $key_value);
if( $within_array == 'yes' ){
break;
}
} else {
if( $v == $key_value && $k == $key ){
$within_array = 'yes';
break;
}
}
}
return $within_array;
}
$test = array(
0=> array('ID'=>1, 'name'=>"Smith"),
1=> array('ID'=>2, 'name'=>"John")
);
print_r(is_in_array($test, 'name', 'Smith'));
?>

How to Perform in_array for Associative Arrays in PHP

Both in_array() and array_search() work just fine for associative arrays.

See these examples:

php > $array = ['one' => 1, 'two' => 2, 'three' => 3];

php > var_dump(in_array(2, $array));
bool(true)

php > var_dump(in_array(5, $array));
bool(false)

php > var_dump(array_search(2, $array));
string(3) "two"

php > var_dump(array_search(5, $array));
bool(false)

in_array() doesn't work as expected with associative array

Your problem is your if statement:

if (in_array($form_email, $client_emails))

Here you search the email in the values ([NULL, "page_two", "page_three", NULL]), but you need to look into the keys (["email@ex-one.com", ..., "email@ex-four.com"]), so just use array_keys(), e.g.

if (in_array($form_email, array_keys($client_emails)))
//^^^^^^^^^^^ See here, so you serach the email in the keys

Can't get in_array to work with associative array

You're looking for array_key_exists(), not in_array(), since you are searching for a specific key, not searching the values:

if( array_key_exists('1001', $products))

Using in_array inside a foreach to filter associative array

To use in_array function you had to insert the key you are looking for (needle) as first parameter and the array as second parameter.

See php documentation for further information.

Here is the code with the correct call to the in_array function.

foreach ($central as $key => $value) {
$key = str_replace("_", " ", $key);
if(in_array($key, $filter)){
echo "<ul>".ucwords($key).':' .' '. $value."</ul>";
}
}

Note that continue statement is useless because is at the end of the for loop.

check and compare associative array value with in_array?

Associative array consist of key, column and values. So, in order to check existence of value you need to reach key of array.

for($i=0;$i<count($_SESSION["cart_item"]);$i++)
{
if( in_array( 556729685 ,$_SESSION["cart_item"][$i] ) )
{
echo "exist in id:".$i.'<br/>';
}
}

PHP in_array() associative array issue

Since you have a die() both in the if and the else-block, your code will only test the first iteration and then stop executing. That's why it works when you omit the else.

Change it to:

for ($j=0; $j<count($csv); $j++) {
if (in_array($code, $csv[$j])) {
$output = json_encode(array('type'=>'message', 'text' => 'Found'));
die($output);
}
}

$output = json_encode(array('type'=>'error', 'text' => 'Not Found'));
die($output);

Since you have a die() in the if-block, any code after the loop will only be executed if the expression in the if-block never evaluates as true.

PHP in_array not working properly?

You are re-initialising your array on every iteration of the while loop. You should declare it outside of the loop:

$Res_Array = array();
while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
$SQL_Result_Time = $Result_Data_2['Interpretation_Time'];

/* Some statements here */

if(in_array($SQL_Result_Time, $Res_Array, true)){
break;
}
else{
array_push($Res_Array, $Number, $SQL_Questionnaire_ID, $SQL_User_ID, $SQL_Psychology_FirstName, $SQL_Psychology_LastName, $SQL_Result_Date, $SQL_Result_Time);
}
echo "<pre>";print_r($Res_Array);echo "</pre>";
}

Also, as mentioned by Marvin Fischer in his answer, your break statement will terminate the while loop on the first duplicated value. You should instead use continue

while ($Result_Data_2 = mysqli_fetch_array($Result)){ //130 rows from database
...
if(in_array($SQL_Result_Time, $Res_Array, true)){
continue;
}
....
}

This question should clarify any issues you have with break and continue statements



Related Topics



Leave a reply



Submit