简体   繁体   中英

How to dig through a multidimentional array?

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
  )
)

...yeah. I need to search through these and pull out the RATE value, which here is 11.35.

For/each loops have failed me so far, any other ideas?

Here is a function to do this for you:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM