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.
Instead of using
if(in_array($needle, $haystack)) {
echo $haystack['actorNoms'] . '<br />' ;
}
try this:
if(in_array($needle, $haystack['actorNoms'])) {
echo $haystack['actorName'] . '<br />' ;
}
what you did was search the $haystack
which is the main array for the actors.
in_array
doesn't search nested arrays automatically for multidimensional arrays, hence you need to specify the area in which you would search: in_array($haystack['actorNoms'])
$needle = 'cyclops';
foreach($arrActors as $haystack)
{
if(in_array($needle, $haystack['actorNoms'])) {
echo $haystack['actorName'] . '<br />' ;
}
}
in_array, works for one level array only. So every time it goes through first level array, where as 'actorNoms' is sub array under firstlevel array.
Try this this give you parent index using this index you will get the data
$arrActors = array( array(
'actorName' => "heath ledger",
'actorAlias' => array(),
'actorGender' => 1,
'actorNoms' => array("angel", "john constantine")
),array(
'actorName' => "Michael pare",
'actorAlias' => array("mikey", "that guy"),
'actorGender' => 1,
'actorNoms' => array("cyclops", "slim", "eric the red")
)
);
print_r( getParent_id("cyclops",$arrActors));
function getParent_id($child, $stack) {
foreach ($stack as $k => $v) {
if (is_array($v)) {
// If the current element of the array is an array, recurse it and capture the return
$return = getParent_id($child, $v);
// If the return is an array, stack it and return it
if (is_array($return)) {
return array($k => $return);
}
} else {
// Since we are not on an array, compare directly
if (preg_match("/$child/",$v)) {
// And if we match, stack it and return it
return array($k => $child);
}
}
}
// Return false since there was nothing found
return false;
}