0

I have an array

Array(
[32] => ki
[97] => Array
    (
        [0] => l$
        [1] => ml
        [2] => 8e
    )
[98] => fp
[99] => @w
[100] => lf 
)

if I do array search for example:

echo array_search("fp", $array);

the output will be "98". How can I get the key if im looking for a value inside another array like "ml"? I wanted to get "97" if i search for value "ml".

Jhay
  • 76
  • 6

2 Answers2

0

I don't think there is such function for multi arrays

If you want to do it in a loop try:

foreach($array as $key => $value)
{
    if(is_array($value))
    {
        $subarray = $value;

        foreach($subarray as $subvalue)
        {
            if($subvalue == 'ml')
            {
                echo $key;
                break 2;
            }
        }
    }
    else
    {
        if($value == 'ml')
        {
            echo $key;
            break;
        }
    }
}
Eddy Unruh
  • 576
  • 1
  • 5
  • 18
0

You could write an alternate recursive array_search function like this:

function recursive_array_search($needle, $haystack, $parent_key = null) {
    foreach($haystack as $key => $value) {
        $current_key = $parent_key ? $parent_key : $key;
        if($needle === $value || (is_array($value) && recursive_array_search($needle, $value, $current_key) !== false)) {
            return $current_key;
        }
    }
    return false;
}

And call it like echo recursive_array_search("ml", $array);

Based on http://php.net/manual/en/function.array-search.php#91365

Rockholla
  • 61
  • 3