如何检查多维数组php

时间:2018-10-09 09:54:36

标签: php

我要在此数组中搜索 50075285

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

        )

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

        )

)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试此功能:

该值应与$ key数组键相关联

您要在数组中搜索的$ value值

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;
}