如何从多维数组中回显字符串值

时间:2015-01-08 07:09:03

标签: php arrays sorting

如何从下面的多维数组中回显每个hotelCode, totalPrice, boardTyperoomCategory

<?php
$hotelCodes = array() ;
$availHotels = $checkAvailability->availableHotels ;
foreach($availHotels as $hotel){
    if (!isset($hotelCodes[$hotel->hotelCode])) {
        $hotelCodes[$hotel->hotelCode] = array();
    }
    // you could ofcourse just take specific info from $hotel and put it in here.
    $hotelCodes[$hotel->hotelCode][] = $hotel;
}
?>

和     print_r of $hotelCodes

Array ( [HOTELCODE1] => Array ( [0] => stdClass Object ( [processId] => HV-82768336 [hotelCode] => ITULJP [availabilityStatus] => InstantConfirmation [totalPrice] => 279 [totalTax] => 0 [totalSalePrice] => 298.86 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Twin Room - Non-Refundable-1 double bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 279 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 34.84 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 34.88 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 34.88 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 34.88 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 34.88 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 34.88 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 34.88 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 34.88 ) ) ) ) ) [1] => stdClass Object ( [processId] => HN-10616086 [hotelCode] => ITULJP [availabilityStatus] => InstantConfirmation [totalPrice] => 294 [totalTax] => 0 [totalSalePrice] => 314.59 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Double or Twin Room-1 double bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 294 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 36.75 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 36.75 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 36.75 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 36.75 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 36.75 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 36.75 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 36.75 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 36.75 ) ) ) ) ) ) [HOTELCODE2] => Array ( [0] => stdClass Object ( [processId] => HZ-21733446 [hotelCode] => ITOI6P [availabilityStatus] => InstantConfirmation [totalPrice] => 295 [totalTax] => 0 [totalSalePrice] => 0 [currency] => EUR [boardType] => Room Only [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Double Or Twin Standard [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 295 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 37 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 37 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 37 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 37 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 37 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 37 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 37 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 36 ) ) ) ) ) ) )

我需要回应价值观 HOTELCODE1和HOTELCODE1为$ hotelcode [1]和$ hotelcode [2] 每个酒店代码的totalRoomRate,roomCategory和boardType

由于

1 个答案:

答案 0 :(得分:0)

您已经在那里,您只需要确保只为酒店代码存储一家酒店。

foreach($availHotels as $hotel){
    // if we do not have a hotel with this code yet
    if (!isset($hotelCodes[$hotel->hotelCode])) {
        // store this one there
        $hotelCodes[$hotel->hotelCode] = $hotel;
    }
}

// run over the available hotels
foreach( $hotelCodes as $hotel ) {
  echo $hotel['hotelCode'] . ' -> ' . $hotel['totalPrice' ] . ' -> etc ';
}