0

i need to translate this json into a php array then cycle through the items and put them into there respective columns i have tried using json decode on it own but all i get back is the text array i know the request works fine and gives the following output :

 [{"roomType":"Single rooms","roomTypeCode":"SB"},{"roomType":"Double rooms","roomTypeCode":"DB"},{"roomType":"Twin rooms","roomTypeCode":"TB"},{"roomType":"Triple rooms","roomTypeCode":"TR"},{"roomType":"Suites","roomTypeCode":"S"},{"roomType":"Non-smoking rooms","roomTypeCode":"NS"},{"roomType":"Quad rooms","roomTypeCode":"Q"},{"roomType":"Twin for sole use","roomTypeCode":"TS"}]

if i print the string that needs to be decoded its all there but json decode doesn't seem to be able to just but this into an associative array the way im trying to use it heres the request

$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdatar = curl_exec($ch);
curl_close($ch);
$roomdata = json_decode($rawdatar);
print $roomdata;

if i print $rawdatar i get the aforementioned line of json but nothing but Array when i print $roomdata what am i doing wrong

1
  • 1
    Use var_dump() to see what you got instead of print. Commented Aug 10, 2012 at 18:40

2 Answers 2

2

Your JSON data is an array of objects:

Try doing this:

echo $roomdata[0]->roomType;

if you print($roomdata), it will always show Array because that is what it is. For more information, read up on json_decode: http://php.net/manual/en/function.json-decode.php

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

Comments

1

Decoding JSON is not going to magically put things into an associative array. The JSON structure you have there represents an array of objects. You would need to map this to an associative array yourself like this:

$object_array = json_decode($rawdatar);
$result_array = array();
foreach($object_array as $obj) {
    $result_array[$obj->roomType] = $obj->roomTypeCode;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.