I have an array and i am trying to decode and parse the json values,
can't get it right.
Here's the info:
$send[0] :
Array ( [0] => {"message-count":"1","messages":[{"error-text":"Missing to param","status":"2"}]} )
var_dump(json_decode($v_send[0]));
/* output
json Dunmpobject(stdClass)#1 (2) { ["message-count"]=> string(1) "1" ["messages"]=> array(1) { [0]=> object(stdClass)#2 (2) { ["error-text"]=> string(16) "Missing to param" ["status"]=> string(1) "2" } } }
*/
var_dump(json_decode($v_send[0], true));
/* output
array(2) { ["message-count"]=> string(1) "1" ["messages"]=> array(1) { [0]=> array(2) { ["error-text"]=> string(16) "Missing to param" ["status"]=> string(1) "2" } } }
*/
$json=json_decode($v_send[0]);
echo "Start:";
echo "<br/><br/>";
// To loop
if (!is_array($json)) die('...');
foreach ($json as $key=>$tts_result)
{
echo $tts_result->callid;
echo "<br/><br/>";
echo $tts_result->to;
echo "<br/><br/>";
echo $tts_result->messages["status"];
echo "<br/><br/>";
echo $tts_result->error-text;
}
the echo in the loop gives empty result. anyone can help ?
$json is not an array, it's an object (of class stdClass).
If you want arrays, pass true as second argument of json_decode:
$json = json_decode($v_send[0], true);
Related
I'm a little confused I have the following;
array(4) { ["id"]=> string(1) "2" ["result"]=> array(5) { ["account"]=> string(34) "rf2DKsfbZuCa2WwQAg49cRXqYuRCTszUTp" ["assets"]=> array(1) { ["rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ"]=> array(1) { [0]=> array(2) { ["currency"]=> string(3) "FLX" ["value"]=> string(9) "999999999" } } } ["ledger_hash"]=> string(64) "E44084AAC1F1AE2C7147675BF8F763ED7620DC82E1B1A5BD6DC6F640652F1B63" ["ledger_index"]=> int(17773072) ["validated"]=> bool(true) } ["status"]=> string(7) "success" ["type"]=> string(8) "response" }
I am trying to echo the currency;
I have the following but it's not working.
$response = websocket_read($sp,$errstr);
$decode = json_decode($response, true);
echo'<br><br>';
echo $decode['result']['assets']['currency'];
echo'<br><br>';
echo "Server responed with: '" . $response ."'\n";
The line that is not working is echo $decode['result']['assets']['currency'];
Thankyou
Your rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ has the value as an array, either remove the square brackets from this part to treat it as value
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":[{"currency":"FLX","value":"999999999"}]
to
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":{"currency":"FLX","value":"999999999"}
or use index to get the value
echo $decode['result']['assets']['rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ'][0]['currency'];
I have a JSON file I want to fill in a table, but can't quite figure out how to retrieve the data inside and loop it through an array to get in a table I made.
This is my JSON file: test.JSON:
{
"data":{
"Chair":{
"id":24,"key":"Chair","name":"Chair","title":"oak home made"
},
"Table":{
"id":37,"key":"Table","name":"Table","title":"round white table"
},
"Closet":{
"id":18,"key":"Closet","name":"Closet","title":"big and red"
},
"Sofa":{
"id":110,"key":"Sofa","name":"Sofa","title":"room for five persons"
}
},
"type":"furniture","version":"1.1.0"
}
Then with PHP I used this: test.PHP:
$url = 'test.json';
$result=file_get_contents($url);
$decoded=json_decode($result, true);
var_dump($decoded);
This is what I get:
array(3) { ["data"]=> array(4)
{
["Chair"]=> array(4)
{ ["id"]=> int(24) ["key"]=> string(5) "Chair" ["name"]=> string(5) "Chair" ["title"]=> string(13) "oak home made" }
["Table"]=> array(4)
{ ["id"]=> int(37) ["key"]=> string(5) "Table" ["name"]=> string(5) "Table" ["title"]=> string(17) "round white table" }
["Closet"]=> array(4)
{ ["id"]=> int(18) ["key"]=> string(6) "Closet" ["name"]=> string(6) "Closet" ["title"]=> string(11) "big and red" }
["Sofa"]=> array(4)
{ ["id"]=> int(110) ["key"]=> string(4) "Sofa" ["name"]=> string(4) "Sofa" ["title"]=> string(21) "room for five persons" }
}
["type"]=> string(9) "furniture" ["version"]=> string(5) "1.1.0"
}
Then I am trying to retrieve "Chair", "Table", "Closet" and "Sofa", but I don't know how to do it. When I try this:
foreach ($decoded as $key => $value) {
echo "key: ".$key;
echo "</br></br>";
echo "value: ".$value;
echo "</br></br>";
return;
}
I get:
key: data
value: Array
Can someone help me get "Chair", "Table", "Closet" and "Sofa", including each of thoses' id, key, name and title?
I have received guides, but they won't help me because I feel like this type of "array" is different then the links for guides I receive.
Thanks!
You are looping through the array on the top most level, while you want to be a level lower than that to be able to loop through all the elements of data, like this:
foreach ($summonerDecoded2['data'] as $key => $value)
{
echo "key: ".$key."</br>";
echo "value id: ".$value['id']."</br>";
echo "value key: ".$value['key']."</br>";
echo "value name: ".$value['name']."</br>";
echo "value title: ".$value['title']."</br>";
}
You can just do this:
$url = 'test.json';
$result=file_get_contents($url);
$decoded = json_decode($result, true);
foreach ( $decoded["data"] as $key => $value) {
echo $value[ "key" ];
echo "<br />";
//You can access data by:
/*
echo $value[ "id" ];
echo $value[ "key" ];
echo $value[ "name" ];
echo $value[ "title" ];
*/
}
This will result to:
Chair
Table
Closet
Sofa
I have a problem with the follow php object dump. I try to check if "price" is an array, php return me always false. var_dump() of array "price" return me first element of array and not the entire array. Which is wrong? How can I check if it is an array?
object(SimpleXMLElement)#197 (1) {
["price"]=> array(4) {
[0]=> object(SimpleXMLElement)#156 (4) {
["room_id"]=> string(4) "1755"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(4) "5842"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(4) "2450"
}
}
[1]=> object(SimpleXMLElement)#143 (4) {
["room_id"]=> string(5) "24206"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(4) "5842"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(4) "2450"
}
}
[2]=> object(SimpleXMLElement)#135 (4) {
["room_id"]=> string(4) "1755"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(6) "415725"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(6) "2327.5"
}
}
[3]=> object(SimpleXMLElement)#136 (4) {
["room_id"]=> string(5) "24206"
["room_seq"]=> string(1) "1"
["offer_id"]=> string(6) "415725"
["price_total"]=> object(SimpleXMLElement)#205 (2) {
["price_typ"]=> string(1) "0"
["price_hb"]=> string(6) "2327.5"
}
}
}
}
Consider a simplified example:
<root>
<element>first</element>
<element>second</element>
<element>third</element>
</root>
$root->element is not really an array. It is SimpleXMLElement object. You can think of it as a selector, a collection of elements you can traverse using foreach and also access specific object using indexes:
$root->element[0]; //first object
$root->element[1]; //second
$root->element[2]; //third
Here is more examples of basic usage: http://php.net/manual/en/simplexml.examples-basic.php
So the real question is: How to define if there is more than one element in collection ?
You can do it using count() method:
if($es->result->hotel->channel->room_price->price->count() > 1){
echo 'many elements';
}
Read more: http://php.net/manual/en/simplexmlelement.count.php
// simplexml_load_string() return false if not $XML
$xml = simplexml_load_string($pSdata);
if(gettype($xml)=='object')
{
print_r(simplexmlArray($xml));
} else {
echo "no xml input";
}
// simplexml_load $xml obj to array
function simplexmlArray ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? simplexmlArray ( $node ) : $node;
return $out;
}
I returned an array of JSON data type from javascript to PHP, I used json_decode($data, true) to convert it to an associative array, but when I try to use it using the associative index, I get the error "Undefined index" The returned data looks like this
array(14) { [0]=> array(4) { ["id"]=> string(3) "597" ["c_name"]=> string(4) "John" ["next_of_kin"]=> string(10) "5874594793" ["seat_no"]=> string(1) "4" }
[1]=> array(4) { ["id"]=> string(3) "599" ["c_name"]=> string(6) "George" ["next_of_kin"]=> string(7) "6544539" ["seat_no"]=> string(1) "2" }
[2]=> array(4) { ["id"]=> string(3) "601" ["c_name"]=> string(5) "Emeka" ["next_of_kin"]=> string(10) "5457394839" ["seat_no"]=> string(1) "9" }
[3]=> array(4) { ["id"]=> string(3) "603" ["c_name"]=> string(8) "Chijioke" ["next_of_kin"]=> string(9) "653487309" ["seat_no"]=> string(1) "1" }
Please, how do I access such array in PHP? Thanks for any suggestion.
As you're passing true as the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
$myArray = json_decode($data, true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['c_name']; // Fetches the first c_name
// ...
echo $myArray[2]['id']; // Fetches the third ID
// etc..
If you do NOT pass true as the second parameter to json_decode it would instead return it as an object:
echo $myArray[0]->id;
$data = json_decode($json, true);
echo $data[0]["c_name"]; // "John"
$data = json_decode($json);
echo $data[0]->c_name; // "John"
$data = json_decode(...);
$firstId = $data[0]["id"];
$secondSeatNo = $data[1]["seat_no"];
Just like this :)
As you're passing true as the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
This may help you!
$latlng='{"lat":29.5345741,"lng":75.0342196}';
$latlng=json_decode($latlng,TRUE); // array
echo "Lat=".$latlng['lat'];
echo '<br/>';
echo "Lng=".$latlng['lng'];
echo '<br/>';
$latlng2='{"lat":29.5345741,"lng":75.0342196}';
$latlng2=json_decode($latlng2); // object
echo "Lat=".$latlng2->lat;
echo '<br/>';
echo "Lng=".$latlng2->lng;
echo '<br/>';
When you want to loop into a multiple dimensions array, you can use foreach like this:
foreach($data as $users){
foreach($users as $user){
echo $user['id'].' '.$user['c_name'].' '.$user['seat_no'].'<br/>';
}
}
I want to create a JSON object from my MySQL results with PHP so I can pass it to JavaScript. I don't quite understand the difference between JSON array and JSON object.
This is how I do it. But is there a better way? This is the array way I believe?
$json = array();
while($r=mysql_fetch_array($res)){
$json['firstname'] = $r['firstname'];
$json['lastname'] = $r['lastname'];
}
echo json_encode($json);
I want to be able to get the info from JavaScript, by selecting all first names only If I wish etc..
you can try this, fetch data and push to array, then echo that array
$info=array();
while($row = mysql_fetch_array($res,MYSQL_ASSOC)){
array_push($info,$row);
}
echo json_encode($info);
would return
array(2) { [0]=> array(3) { ["id"]=> string(1) "1" ["firstname"]=> string(3) "foo" ["lastname"]=> string(3) "bar" } [1]=> array(3) { ["id"]=> string(1) "2" ["firstname"]=> string(3) "foo" ["lastname"]=> string(3) "bar" } }
json
[{"id":"1","firstname":"foo","lastname":"bar"},{"id":"2","firstname":"foo","lastname":"bar"}]
Well this would encode every row, with each row being the JSON Object:
$json = array();
while($r=mysql_fetch_array($res)){
$json[] = $r;
}
echo json_encode($json);