I have a php array with mixed data types (arrays, ints, strings). I want to search the array for a match contained within an array of mixed data types as show below.
my test array
$arrActors =[0 => [
'actorName' => "heath ledger",
'actorAlias' => [],
'actorGender' => 1,
'actorNoms' => ["angel", "john constantine"]
],
1 => [
'actorName' => "Michael pare",
'actorAlias' => ["mikey", "that guy"],
'actorGender' => 1,
'actorNoms' => ["cyclops", "slim", "eric the red"]
]
];
If the needle is set to an element and that element is found to exists in actorNoms, I want to echo back the name of the associated actor (actorName). In the below example, I have attempted to find cyclops (actorNoms) return the name of the actor, Michael Pare (actorName) who is associated with him.
My Attempt to find actorNoms and return the actors name
$needle = 'cyclops';
foreach($arrActors as $haystack)
{
if(in_array($needle, $haystack)) {
echo $haystack['actorNoms'] . '<br />' ;
}else{
echo 'nothing found<br />';//echo something so i know it ran
}
}
My attempt returns fails as it echo's 'nothing found'. How do I echo back the name of the actor Michael Pare when searching for cyclops.
Thank you for any help given. I have tried to format my code correctly for ease of use. I have searched Stack, Google and other sources for several hours now trying to find a solution I can understand. I am not very adept, but I promise I am learning and all help is appreciated.