-1

I want to search 50075285 in this array.

$xyz=Array
(
    [typeA] => Array
        (
            [details_typeA] => Array
                (
                    [id] => 50075285
                    [action_code] => PDF_ONLINE   
                )

        )

    [typeB] => Array
        (
            [details_typeB] => Array
                (
                   [id] => 50075287
                   [action_code] => offline
                )

        )

)
Lahiru Jayaratne
  • 1,684
  • 4
  • 31
  • 35
  • 1
    Please write an appropriate question, not just a dump – Lithilion Oct 09 '18 at 09:57
  • If i am not wrong its a 3 dimensional array – user10477731 Oct 09 '18 at 09:57
  • Hello and welcome to SO! Unfortunately it's not really clear what you are asking. Are you wanting to do a value search on all elements or only specific keys? What have you tried so far? – Bananaapple Oct 09 '18 at 10:01
  • Also, have a read of this https://steemit.com/php/@crell/php-use-associative-arrays-basically-never and then this https://steemit.com/php/@crell/php-never-type-hint-on-arrays for some food for thought on using associative arrays. – Bananaapple Oct 09 '18 at 10:08
  • yes i want to do a value search for example if i want to check 50075285 is present or not – user10477731 Oct 09 '18 at 10:19

2 Answers2

0
//this gives the exact value in a specific array
//in this case this is the value you want. 
$value = $xyz['typeA']['details_typeA']['id'];

OR

//this gives every id in the `details_typeX` array
foreach($xyz as $type){
    foreach($type as $details){
        $value = $details['id']
    }
}

PHP Sandbox proof

Gobbin
  • 530
  • 3
  • 17
0

Try this function:

$key array key the value should be associated with

$value value you are searching the array for

function arr_search($array, $key, $value)
{
    $results = array();

    if (is_object($array)){ $array = (array)$array; }

    if (is_array($array))
    {
        if (isset($array[$key]) && $array[$key] == $value)
            $results[] = $array;

        foreach ($array as $subarray)
            $results = array_merge($results, arr_search($subarray, $key, $value));
    }

    return $results;
}
Mehdi
  • 661
  • 5
  • 17