解析多维数组并拉取值

时间:2015-01-27 11:13:27

标签: php arrays

如果$array[productType]匹配,我如何解析下面的多维数组([totalPrice])并提取[productCategory][packageCode]$pkgcodes[1]...[z]的值值$pkgcodes $pkgcodes是一个代码数组

Array ( [0] => TRA1I2 [1] => TREZEC [n] ...)

的print_r
$array

数组$array是SOAP客户端的响应

Array ( [0] => Array ( [packageCode] => TRA1I2 [totalPrice] => 17 [productType] => product Only [products] => Array ( [0] => Array ( [productCategory] => Simple [paxes] => Array ( [0] => Array ( [paxType] => Adult [age] => 30 ) [1] => Array ( [paxType] => Adult [age] => 30 ) ) [totalproductRate] => 17 [ratesPerNight] => Array ( [0] => Array ( [date] => 2015-01-28 [amount] => 17 ) ) ) ) ) [1] => Array ( [packageCode] => TREZEC [totalPrice] => 17 [productType] => product Only [products] => Array ( [0] => Array ( [productCategory] => Complicated [paxes] => Array ( [0] => Array ( [paxType] => Adult [age] => 30 ) [1] => Array ( [paxType] => Adult [age] => 30 ) ) [totalproductRate] => 17 [ratesPerNight] => Array ( [0] => Array ( [date] => 2015-01-28 [amount] => 17 ) ) ) ) ) ).

的print_r
{{1}}

你更感谢你的帮助

1 个答案:

答案 0 :(得分:0)

尝试 -

$newArr = array();
foreach($array as $value) {
   if (in_array($value['packageCode'], $pkgcodes)) {
       $temp['productType'] = $value['productType'];
       $temp['totalPrice'] = $value['totalPrice'];
       $temp['packageCode'] = $value['packageCode'];
       $temp['productCategory'] = $value['products']['productCategory'];
       $newArr[] = $temp;
   }
}
var_dump($newArr);