2

I would like to push an associate array into another array but I an not sure how to go about it. At the minute I have the following:

$rate_info = array(
    "hotel_rating" => $hotel->{'hotelRating'},
    "room_rate" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'@total'},
    "currency" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'@currencyCode'},
    "deep_link" => $hotel->{'deepLink'}
);

array_push($hotel_array[$hotel->{'name'}]["offers"],  "expedia" => $rate_info );

"Offers" is an array , all I want to do is add an key value with an array within in. Any ideas? All I seem to be getting at the minute is parse errors.

UPDATE

This is the output of the array so far

["offers"]=>
array(2) {
  ["LateRooms"]=>
  array(4) {
    ["hotel_rating"]=>
    int(4)
    ["room_rate"]=>
    string(6) "225.06"
    ["currency"]=>
    string(3) "USD"
  }
  [0]=>
  string(4) "test"
}

As you can see instad of [0] I would like ["site"]=>array()

Thanks

Oliver

6
  • What bit is generating parse errors? What errors are you getting? Commented May 23, 2012 at 18:18
  • 1
    Incidentally, you can replace $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'} (etc) with $hotel->RoomRateDetailsList->RoomRateDetails (etc). The last one on the end (@total) looks very dodgy too - is the @ necessary? Commented May 23, 2012 at 18:19
  • It is sending back the parse error for the line: array_push($hotel_array[$hotel->{'name'}]["offers"], "expedia" => $rate_info ); Commented May 23, 2012 at 18:20
  • Thanks, the @ sign is the name of the field sent back by the api Commented May 23, 2012 at 18:21
  • Comment on your array structure: should 'LateRooms' only have one hotel (i.e a hotel_rating, room_rate, currency)? Or should that be an array of these records, so that you can have several against LateRooms? Commented May 23, 2012 at 18:25

2 Answers 2

1

I'd do this for the array assignment:

$hotel_array[$hotel->name]['offers']['expedia'] = $rate_info;

Ensure your warnings are enabled, so you know arrays (and subarrays) have been set up before you use them.

Sign up to request clarification or add additional context in comments.

Comments

1

Did you first do this?

$hotel_array[$hotel->{'name'}] = array();

And then you can do:

array_push($hotel_array[$hotel->{'name'}]["offers"],  "expedia" => $rate_info );

1 Comment

Hi, see above I have updated the question. The "name" is a key value the contains an array. Within the array there is another array called "offers".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.