PHP How to get data from multiple level array - php

I have this array from JSON file and I want to get data from [url]. Array is saved as variable $data.
stdClass Object
(
[images] => Array
(
[0] => stdClass Object
(
[startdate] => 20190625
[fullstartdate] => 201906250700
[enddate] => 20190626
[url] => /th?id=OHR.SutherlandFalls_ROW5711472757_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp
)
)
)
Result should be like:
echo $data[0]->url; will show the link/value /th?id=...

Your $data variable is not an array, it's an instance of stdClass. Therefore, you can retrieve it like this:
$data->images[0]->url;

Related

how get value feild[ID] stdClass Object in php code

how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID

Php unserialized a serialized json string

After a http post in c# to php am getting an output of the form
in the code i have
public function actionSubmitInspection(){
$data = $_POST;
return (array)$data["check_comments"];
}
now am getting a result of the form
[
"[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
]
as from my try typecasting array doesnt creating the array, How can i convert the serialized string to an array or an object.
use json_decode function.
public function actionSubmitInspection(){
$data = $_POST;
// replace it
//return (array)$data["check_comments"];
return json_decode($data["check_comments"]);
}
Out put will be array of objects.
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
as from my try typecasting array doesnt creating the array
Yes, it creates an array but the array it creates contains the JSON text.
You need to parse the JSON in order to restore the data structures it encodes. PHP provides the function json_decode() for this purpose. I recommend you pass TRUE as the second argument to json_decode() to get back arrays (otherwise it creates stdClass objects that are just arrays with a fancy syntax and limited options for processing).
// Assuming the value of $data['check_comments'] is:
// "[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
$output = json_decode($data['check_comments']);
print_r($output);
The output:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)
You should use json_decode($data["check_comments"]) the output will be an array of stdClass objects:
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
or passing true on second param, json_decode($data["check_comments"], true) and the output will be an array of arrays:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)

Get stdClass Object value

I'm trying to get a value from stdClass Object array with no success.
Here is the code I'm running:
$myjson =
'{"2":{"label":"","value":"","type":null,"validation":null,"required":null},
"6":{"label":"files","value":"getThisValue","type":"file0","validation":null,"required":null},
"3":{"label":"location","value":"val3","type":"hidden","validation":"","required":"0"}
,"0":{"custom3":"zz","value":"","label":""},"1":{"custom3":"zz","value":"","label":""}
}';
$json = json_decode($myjson);
echo $json[6]->'value';
This is doesn't work, If I Print_r the JSON after decoding (print_r($json)), the array will look like this:
stdClass Object (
[2] => stdClass Object ( [label] => [value] =>
[type] => [validation] => [required] => )
[6] => stdClass Object (
[label] => files [value] => getThisValue [type] => file0 [validation]
=> [required] => )
[3] => stdClass Object ( [label] => location [value] => val3 [type] => hidden [validation] => [required] => 0 )
[0]
=> stdClass Object ( [custom3] => zz [value] => [label] => )
[1] => stdClass Object ( [custom3] => zz [value] => [label] => ) )
I need the Value: getThisValue. Any idea how I can get it? (I tried many options with no success).
Try echo $json["6"]["value"];
But for this you have to use json_decode($myjson, true); true, to get an array.
Because it's going to be two arrays inside each other and not an object you have to use 2 brackets.
You can't use a std object as an array. But in order to get your code working just add this line:
$json = get_object_vars($json);
After this you can access it like this:
echo $json[6]->value;
You could add true to your json_decode, like this: json_decode($myjson, true);, it will now convert your json object to an associative array.
And from there on you can get to the value you want by requesting the key, just like other arrays.
$newArray = json_decode($myjson, true);
echo $newArray['something'];
If you want to get the values from stdObject without converting it to array, you can do it like this:
echo $json->{'6'}->value
You can use the {'property_name'} notation to get a value of a class property with non-standard name (for ex. a number).

PHP - How to read data from SOAP XML

I have code for read XML SOAP 1.2 from web service.
Im using this: https://stackoverflow.com/a/18580428/2629513
I get this code below:
SimpleXMLElement Object
(
[OdkazyResponse] => SimpleXMLElement Object
(
[OdkazyResult] => SimpleXMLElement Object
(
[odkazy] => SimpleXMLElement Object
(
[odkaz] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => 31400001
[typ] => OBR1
[popis] => Oki ML 280 - foto
[url] => http://www.atcomp.cz/katalog/31400001/ML280.gif
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => EC376123GB
[typ] => OBR1
[popis] => Malý obrázek
[url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_maly.jpg
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => EC376123GB
[typ] => OBR2
[popis] => Velký obrázek
[url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_velky.jpg
)
)
And how I can read the [kod_zbozi], [typ], [popis], [url] attributes? I need to save it into my mysql database (this is not problem, the problem is read the data from this format XML). Thanks.
Well, the very first thing you might want to do is to convert that object into an array (to avoid naming problems)
You can use this function to do that:
function object2array($object) {
return json_decode(json_encode($object), true);
}
then something like this:
$data = object2array(simplexml_load_string('....'));
print_r($data); // Its regular array now, use it keys to access values, then simply insert them into db

How do I extract a value from this json_decode?

I have got this result using json decode from an api call. but I dont know how to extract "VALUE" from this result..
$obj=json_decode($json_string);
print_r($obj);
stdClass Object ( [status] => OK [data] => stdClass Object ( [trends] => stdClass Object ( [rank] => Array ( [0] => stdClass Object ( [date] => 201011 [value] => 7196 ) ) ) [trends_low_sample] => [query_cost] => 1 [trends_frequency] => monthly ) )
I need only "7196" from this result. how do I do this??
Ah! Based on your updated code you're tring to get the value from PHP not Javascript? Personally I use json_decode($json_string,true); to get an associative array (json_decode), if you do that it should be accessible as:
echo $obj["data"]["trends"]["rank"][0]["value"];
As an object it's accessible as:
echo $obj->data->trends->rank[0]->value;

Categories