If I have an associative array that stores values from csv file like this:
$file_users = fopen("Users.csv", "r");
while (($record = fgetcsv($file_users, 1024, ',""')) !== FALSE){
$users[]= array($record[0] => $record[2].$record[3]);
}
record[0], record[2] and record[3] are the values from the columns, row by row.
Then this is my way to search a value from the array and print the result:
for($i=0;$i<count($users);$i++){
foreach($users[$i] as $username_matricula => $username_fullname){
if($username_matricula === $some_string){
echo "Found: ".$some_string." and the result is: ".$username_fullname;
}#end if
}#end foreach
}#end for
And it throws me the following warning:
Warning: Invalid argument supplied for foreach().
What am I doing wrong? Is there another way to find a string in assoc array?
perhaps this?
by adding (array) in front of the actual array you make it empty-safe. (no warnings)