I've never really used PHP arrays before as I've just used a database in the past so I'm broadening my horizons a little. Basically I have a simple nested array where each element has a 'name' and some other values. I'm trying to search through the array, which is part of an object. I've looked through a number of previous questions on here and can't get it working, although in the other cases there haven't been objects involved. I've been trying to use a 'needle/haystack' type example but haven't got it working yet.
So in my People class we have among other things:
public $peopleArray; // this is the array and will be protected once working
// and this is the example search function im trying to modify
public function findPerson($needle, $haystack)
{
foreach($haystack as $key=>$value)
{
if(is_array($value) && array_search($needle, $value) !== false)
{
return $key;
}
}
return 'false';
}
And then to call this I currently have:
$searchResult = $People->findPerson('Bob',$people->peopleArray,'name');
I'm not sure if I'm just confusing myself with what the $needle and $value - I need to pass the name value in the search function, so I did have $value in the function arguments, but this still returned nothing. Also I'm not 100% on whether '$key=>$value' needs modifying as $key is undefined.
Thanks in advance for any help.
Addition - print_r of the array:
Array ( [0] => Person Object ( [id:protected] => 1 [name] => Bob [gender] => m )
[1] => Person Object ( [id:protected] => 2 [name] => Denise [gender] => f )
[2] => Person Object ( [id:protected] => 3 [name] => Madge [gender] => f ) )
print_r()of the$peopleArrayto your questionarray_search($needle, $value) !== falseshould bearray_search($needle, $value) != false)