如何挖掘多维数组?

时间:2012-02-17 16:16:05

标签: php arrays multidimensional-array foreach

Array (
  [0] => Array (
    [PACKAGE] => Array (
      [0] => Array (
        [ATTRIBUTES] => Array (
          [ID] => 0
        )
        [ZIPORIGINATION] => Array (
          [0] => Array (
            [VALUE] => 32751
          )
        )
        [ZIPDESTINATION] => Array (
          [0] => Array (
            [VALUE] => 55967
          )
        )
        [POUNDS] => Array (
          [0] => Array (
            [VALUE] => 0
          )
        )
        [OUNCES] => Array (
          [0] => Array (
            [VALUE] => 9
          )
        )
        [CONTAINER] => Array (
          [0] => Array (
            [VALUE] => Flat Rate Box
          )
        )
        [SIZE] => Array (
          [0] => Array (
            [VALUE] => REGULAR
          )
        )
        [ZONE] => Array (
          [0] => Array (
            [VALUE] => 6
          )
        )
        [POSTAGE] => Array (
          [0] => Array (
            [MAILSERVICE] => Array (
              [0] => Array (
                [VALUE] => Priority Mail<sup>&reg;</sup> Medium Flat Rate Box
              )
            )
            [RATE] => Array (
              [0] => Array (
                [VALUE] => 11.35
              )
            )
          )
        )
      )
    )
  )
)

Array (
  [0] => price Object (
    [mailservice] => Priority Mail<sup>&reg;</sup> Medium Flat Rate Box
    [rate] => 11.35
  )
)

...耶。我需要搜索这些并拉出RATE值,这里是11.35。

对于/每个循环到目前为止我都失败了,还有其他想法吗?

1 个答案:

答案 0 :(得分:1)

以下是为您执行此操作的功能:

findKey($myarray,$searchKey) {
    foreach($myarray as $key=>$value) {
       if($key==$searchKey) return $value;
       if(is_array($value) || is_object($value)) {
           $returned = findKey($value,$searchKey);
           if($returned) return $returned;
       }
    }
    return false;
}

// call it like this:
findKey($yourArray,"rate")