如果定义了字符串值,则从多维数组中提取数据

时间:2015-01-25 14:05:32

标签: php arrays

如果[totalPrice]值已知,我想提取[boardType][roomCategory][hotelCode]的字符串值。

数组print_r是(只有一家酒店)

Array ( 
[0] => stdClass Object ( 
[processId] => H5-84752260 
[hotelCode] => GRSCDS 
[availabilityStatus] => InstantConfirmation 
[totalPrice] => 40 [totalTax] => 0 
[totalSalePrice] => 0 
[currency] => EUR 
[boardType] => Room and Breakfast (American Buffet Breakfast) 
[rooms] => Array ( 
[0] => stdClass Object ( 
[roomCategory] => Twin Room 
[paxes] => Array ( 
[0] => stdClass Object ( 
[paxType] => Adult 
[age] => 30 ) 
[1] => stdClass Object ( 
[paxType] => Adult 
[age] => 30 ) ) 
[totalRoomRate] => 40 
[ratesPerNight] => Array ( 
[0] => stdClass Object ( 
[date] => 2015-03-11 [amount] => 40 ) ) ) ) ) ) 

1 个答案:

答案 0 :(得分:1)

您可以迭代数组,使用if条件检查项目并获取所需的值。像这样:

<?php
    foreach ($array as $item) {
        if ($item->hotelCode === 'YOUR_CODE') {
            // get your data here using $item->totalPrice, etc
            break;
        }
    }