I am getting a JSON response which looks like this:
stdClass Object
(
[location00] => Array
(
[0] => stdClass Object
(
[id_0] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Wanted by Aryurumoka
[gold_reward] => 58900
[event] => 0
[description] => Not provided.
)
)
)
)
)
For example, i am able to get [name] by $quests->location00[0]->id_0[0]->name.
Lets say i create a new variable $location = 'location00'. Now if i try $quests->$location[0]->id_0[0]->name', i am getting Undefined property: stdClass::$l error. I tried $location = 'location00[0]' as well however i have completly no idea why this happenes. How can i assign location00 to variable to use it while parsing JSON?
You can use associative array or $obj->{$var} :
<?php
$quests = json_decode($json, true);
$location = 'location00';
$name = $quests[$location][0]['id_0'][0]['name'];
I'd try to get new JSON, but you can interpolate object property retrieval with braces:
$quests->{$location}[0]
Related
I cannot find how to get the results from this JSON post in PHP.
stdClass Object
(
[api_job_id] => 398438bf-c0a5-46fc-8774-70d2425e1ce7
[data] => Array
(
[0] => stdClass Object
(
[type] => MESSAGE
[message_id] => 15125005817130024103
[to] => xxx
[error_code] => 0
[#meta] => stdClass Object
(
[error] => stdClass Object
(
[error_desc] => NO_USER
[error_code] => 9
)
)
)
)
)
as you can see the meta has a # icon before it.
I can read all data to vars instead of the data in the #meta
I tried many ways like:
$result = $arrayResponse['#meta']['error']['error_desc'];
it's not working in PHP because of the # icon.
Any idea how I can get the values from these errors in #meta?
To refer to an object attribute with a name that doesn't make a valid variable, you can use braces:
$foo = json_decode($string);
var_dump($foo->{'#meta'});
Or pass a truthy value to json_decode() as the second argument, and you'll get back an array instead of an object:
$foo = json_decode($string, true);
var_dump($foo['#meta']);
I have an array looking something like this:
Array
(
[0] => my_val_one
[1] => my_val_two
)
I then have an object looking something like this:
stdClass Object
(
[id] => 123123
[name] => my_name
[my_val_one] => stdClass Object
(
[my_val_two] => 1
[my_val_three] => 2323
[my_val_four] => 546567
)
)
I want to reference the following object value:
$ob->my_val_one->my_val-two
I'm not sure how to reference this class property from array values that I have.
array_reduce helps here:
$path = ['my_val_one', 'my_val_two'];
$value = array_reduce($path, function ($o, $p) { return $o->$p; }, $ob);
If I have understood correctly, you want to use the string values of the first array to access the StdClass object. You can do this by accessing the attributes dynamically. Here $obj is the StdClass and $arr is your array.
$obj->{$arr[0]}->{$arr[1]}
I have JSON like this (from nested sorting)
[
{"id":13},{"id":14},
{"id":15,
"children":[
{"id":16},{"id":17},{"id":18}
]
},
{"id":19},{"id":20},
{"id":21,
"children":[
{"id":22}
]
}
]
how I PHP loop to put this JSON in MySQL
Thank you.
As with any valid JSON format string, you can use PHP's built-in json_decode to convert it into a parsable object and then loop through those parameters. In this case, from the JSON string you've given (which seems to be an array)
$array = json_decode($string);
foreach ($array as $val) {
//The object $val will be:
"id":13
}
If it's nested, you would do another foreach loop and detect for the property that needs to be looped. For example you could do this in a variety of ways (check for the "children" property, loop through the $val properties and check if it is an array, if it is then loop through that). Inside this foreach loop iteration, you can insert it, or do execute whatever statement you need to get it inside MySQL
Your question is pretty vague on the format and way you want it inserted. I'd suggest showing some code you've tried so other people can help you and know what direction you're going in. (that's probably the reason for the downvote, not mine by the way)
Looping through all the properties of object php
just put your valid json inside json_decode and assign it to a php array as below
//$php_arr = json_decode('YOUR_JSON');
$php_arr = json_decode('[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]');
/*comment the following 3 lines when done*/
echo "<pre>";
print_r($php_arr);
echo "</pre>";
/*comment the above 3 lines when done*/
output
Array
(
[0] => stdClass Object
(
[id] => 13
)
[1] => stdClass Object
(
[id] => 14
)
[2] => stdClass Object
(
[id] => 15
[children] => Array
(
[0] => stdClass Object
(
[id] => 16
)
[1] => stdClass Object
(
[id] => 17
)
[2] => stdClass Object
(
[id] => 18
)
)
)
[3] => stdClass Object
(
[id] => 19
)
[4] => stdClass Object
(
[id] => 20
)
[5] => stdClass Object
(
[id] => 21
[children] => Array
(
[0] => stdClass Object
(
[id] => 22
)
)
)
)
Now as you have PHP array do what ever you want like
foreach($php_arr as $arr){
if(!isset($arr['children'])){
$q = "insert into tbl(id) values('".$arr['id']."')";
}else{
//your logic
}
}
You need to use json_decode function to decode JSON data. Then use foreach loop to manipulate data.
Try example
$str = '
[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]},{"id":19},{"id":20},{"id":21,"children":[{"id":22}]}]
';
$json = json_decode($str);
//var_dump($json);
foreach ($json as $item)
{
//The object $val will be:
echo $item->id."<br />";
//You INSERT query is here
//echo "INSERT INTO table (field) VALUE ($item->id)";
$query = mysqli_query($conn, "INSERT INTO table (field) VALUE ($val->id)");
}
I got this part in an object:
[tcd_old_value] => {"14":{"name":"Nakon radnog vremena","name_changed":false,"added_cc":[],"removed_cc":["4"]},"15":{"name":"Dodatno radno vrijeme","name_changed":false,"added_cc":[],"removed_cc":["4"]}}
after json_decode
$json_object = json_decode(tcd_old_value);
I get:
stdClass Object
(
[14] => stdClass Object
(
[name] => Nakon radnog vremena
[name_changed] =>
[added_cc] => Array
(
)
[removed_cc] => Array
(
[0] => 4
)
)
[15] => stdClass Object
(
[name] => Dodatno radno vrijeme
[name_changed] =>
[added_cc] => Array
(
)
[removed_cc] => Array
(
[0] => 4
)
)
)
I'm trying to count how many indexes are in this object (obviously the result should be 2)
$result = count($json_object);
echo $result //returns 1
Any insight on what I'm doing wrong here?
You cannot use count() in this case, because you have an object and not an array.
You may use the second parameter of json_decode() to have the JSON converted to an associative array:
$json_object = json_decode(tcd_old_value, true);
$result = count($json_object);
echo $result; // Now prints 2
Keep in mind that $json_object is no longer an object, but an array instead.
As per the document count
Returns the number of elements in array_or_countable. If the parameter
is not an array or not an object with implemented Countable interface,
1 will be returned. There is one exception, if array_or_countable is
NULL, 0 will be returned.
You may need to use
json_decode('json', true);
to convert as an array
I have a json response which is decode into an array $data as
stdClass Object ( [outboundSMSMessageRequest] => stdClass Object ( [deliveryInfoList] => stdClass Object ( [deliveryInfo] => stdClass Object ( [address] => 8606142527 [deliveryStatus] => Submitted ) [resourceURL] => http://api-testmobile.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:0f55fd13-a419-4ad9-adec-3dcf63ca39c1/deliveryInfos ) [senderAddress] => OPNHSE [outboundSMSTextMessage] => stdClass Object ( [message] => Sam has requested a payment of Rs 10.00. ) [clientCorrelator] => [receiptRequest] => stdClass Object ( [notifyURL] => [callbackData] => ) [senderName] => [resourceURL] => http://api-openhouse.testingmobile.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:0f5-a419-4ad9-adec-3dcf63ca39c1 ) )
I want to store [deliveryStatus] => Submitted this "Submitted" into a variable.
I have tried $dStatus=$data['deliveryStatus']; but its not working :(
UPDATE
I tried to convert it to associative array by json_decode($data,TRUE);
Array ( [outboundSMSMessageRequest] => Array ( [deliveryInfoList] => Array ( [deliveryInfo] => Array ( [address] => 98989 [deliveryStatus] => Submitted ) [resourceURL] => http://api-otest.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:3b277b5b-cf79-4551-872f-16674499bc09/deliveryInfos ) [senderAddress] => OPNHSE [outboundSMSTextMessage] => Array ( [message] => sam has requested a payment of Rs 100.00 through payt.me . Kindly clickhttps://www.test.me/test to pay. ) [clientCorrelator] => [receiptRequest] => Array ( [notifyURL] => [callbackData] => ) [senderName] => [resourceURL] => http://api-test.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:3b277b5b-cf79-4551-872f-16674499bc09 ) )
I got this.Now how to get the deliveryStatus variable?
If you want to access it a an associative array, you should convert it like an associative array first. Pass TRUE as a second argument to json_decode function as described in docs: http://php.net/json_decode
It's because you're accessing the data in the wrong fashion. json_decode returns an object, so you need to access these fields as object properties. For example:
Instead of
$dStatus=$data['deliveryStatus'];
Try a member access format
$dStatus=$data->deliveryStatus;
If you want to access the data as an associated array, that's also quite simple.
When you call json_decode, pass true as the second parameter:
$myJson = json_decode($data,true);
Please refer to the document on json_decode for more information.
I suggest to look at the view-source of the HTML you are outputting, or to wrap the print_r in a <pre></pre> tag, so that you can see the structure more easily.
Also, the elements are of class Object, which means they are not an array, so you need to use -> to access the elements of your objects.
So if it is an object:
$data = json_decode($response);
$dStatus = $data->outboundSMSMessageRequest->deliveryInfoList->deliveryInfo->deliveryStatus;
If it is an array, then:
$data = json_decode($response, true);
$dStatus = $data['outboundSMSMessageRequest']['deliveryInfoList']['deliveryInfo']['deliveryStatus'];
You see, the deliveryStatus entry is nested in sub-objects in the first case, and in sub-arrays in the second case.