PHP在数组中查找值

时间:2013-09-01 21:32:02

标签: php arrays foreach

我有以下多维数组:

Array
(
[0] => Array
    (
    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 74
                [RecordGUID] => 9BD28E1E-99EB-D4E7-CC2C-AB6F5905BCDA
                [Type] => DENTAL
                [App_Service] => a:4:{i:151;s:6:"AAMCAS";i:152;s:3:"DDS";i:154;s:11:"APP SVC TWO";i:155;s:6:"AADSAS";}
            )

        [1] => Array
            (
                [id] => 73
                [RecordGUID] => A5146CFF-5D17-FB1A-D831-E6835D0A04DD
                [Type] => MED
                [App_Service] => a:1:{i:151;s:6:"AAMCAS";}
            )

        [2] => Array
            (
                [id] => 75
                [RecordGUID] => 0C253109-07E7-151A-0277-19EAC025C2E6
                [Type] => PHYSICAL THERAPY
                [App_Service] => a:1:{i:153;s:8:"PHYSTHER";}
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 155
                [RecordGUID] => 5DF76F3E-2F0C-FD63-B58F-027A61E9BC11
                [AppService] => AADSAS
                [AppService_types] => 
            )

        [1] => Array
            (
                [id] => 151
                [RecordGUID] => 3B503CFC-AB80-C06B-C4C4-8EE548FFC7BF
                [AppService] => AAMCAS
                [AppService_types] => 
            )

        [2] => Array
            (
                [id] => 154
                [RecordGUID] => 753D95F2-6733-AE27-8F2E-48685DC796C0
                [AppService] => APP SVC TWO
                [AppService_types] => 
            )

        [3] => Array
            (
                [id] => 152
                [RecordGUID] => 0D3C9435-64DD-9079-C0F4-D543DFFA0E10
                [AppService] => DDS
                [AppService_types] => 
            )

        [4] => Array
            (
                [id] => 153
                [RecordGUID] => 0D196967-21AF-ADDA-920E-F12938DACADB
                [AppService] => PHYSTHER
                [AppService_types] => 
            )
    )
)

我想找到等于MED的Type键,然后抓住它下面的App_Service值。

我有点卡住了。任何帮助将不胜感激。提前谢谢。

4 个答案:

答案 0 :(得分:0)

那不是多维的,它是一个嵌套结构。数组是关联的,即PHP中的映射。

要找到该值,您可以执行以下操作(其中$ x是您的数组)

foreach($x as $entry) {
     foreach($entry as $subentry) {
         if ($subentry['Type'] == "MED") {
             echo $subentry['App_service'];
         }
     }
}

答案 1 :(得分:0)

试试这个:

foreach($MDArray as $array) {
    if(array_key_exists("Type",$array) {
        foreach($array as $k => $v) {
            if($k == "MED") {
                return $array['AppService'];
            }
        }
    }
}

将$ MDArray替换为多维数组的名称。

答案 2 :(得分:0)

简单的事情通常可以完成工作。

foreach( $array[1] as $find )
{
    if( array_search( 'MED', $find ) )
    {
        echo $find['App_Service'];
    }
}

或者,如果您更喜欢递归函数来检查整个数组:

function findRecursive( $type, $array )
{
    foreach( $array as &$section )
    {
        if( is_array( $section ) )
        {
            if( in_array( $type, $section ) )
            {
                return $section['App_Service'];
            }
            elseif( ( $test = findRecursive( $type, $section ) ) !== FALSE )
            {
                return $test;
            }
        }
    }
    return false;
}

如果您的数组可能没有相同的格式,则该函数对于重用不同类型非常有用。

修改

修正了我的功能。无论如何都不需要array_search。

答案 3 :(得分:0)

如果您的数据可以有几个类型为MED的数组,请尝试以下递归函数:

function findByType($array, $val, &$result) {
    foreach ($array AS $k => $v) {
        if (is_array($v)) {
            if (isset($v['Type']) && $v['Type'] == $val) {
                $result[] = $v['App_Service'];
            } else {
                findByType($v, $val, $result);
            }
        }
    }
}

$result = array();
findByType($data, 'MED', $result);
var_dump($result);