I have been working with parsing some remote JSON with PHP. I have been able to download the JSON and assign it to a variable, and I have used the array functionality with json_decode:
$data = json_decode($remotejson, true);
I have then printed the complete array back to verify the contents of the array:
echo print_r($data);
The array prints back and I can see the keys and values:
[files] => Array
(
[/photogalleryupload.thumbs/1934307_000001.jpg] => Array
(
[source] => derivative
[format] => Thumbnail
[original] => moviefile_1934307.mp4
)
I am trying to get the value of the first nested key name which is "/photogalleryupload.thumbs/1934307_000001.jpg" and assign it to a variable.
For example, I would like the following code:
echo $data['files'][0];
To return this:
/photogalleryupload.thumbs/1934307_000001.jpg
This does not work.
The difficulty I am having is that this value I am trying to return is a 2nd level key name and I have been having trouble finding a way of assigning it to a variable.
$keys = array_keys($data['files'])
$key = $keys[0]
Related
I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.
My array $params (from print_r) looks like this:
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
Cut off here, there are two more objects in this array.
Now if I do: echo $params[0]->name I get an empty result.
Also tried print_r($params[0], true);, empty result.
Also tried, empty result:
$status = $params[0];
echo $status->name;
What am I doing wrong here?
Thanks in advance
Well, as you said your array looks like this :
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name
You need to access object as following
$params['newOrderStatus'];
In above object you will have all child objects so you can access them by following
$params['newOrderStatus']->name;
$params['newOrderStatus']->template;
Your array $params has a key called newOrderStatus which has the object as a value you are looking for.
Looking at your example, there is value for index 0.
To get the value of the name property, you could use:
$params['newOrderStatus']->name
You can type cast it to an array like this:
$array = (array) $yourObject;
i can't get dynamic datas that are contained into an array, here the array
[addresses] => Array
(
[DYNAMIC-ADDRESS] => -249310
)
I'am tryng to get the DYNAMIC-ADDRESS, or rather the text contained on the ['...'] (In this case the text is DYNAMIC-ADDRESS) for get the balance, and for do a check on my database, i have already tried
$ReceiveAddress=$DATA['addresses']['0'];
And then for get the balance
$ReceiveAddress=$DATA['addresses']["$ReceiveBitcoinAddress"];
But this doesn't work...
In case this is the only key-value pair in the array, you can use key.
http://php.net/manual/en/function.key.php
$dynamic_address = key($array);
$value = $array[$dynamic_address];
In case you're handling an array with several elements, Consider using foreach loop.
[addresses] => Array
(
[DYNAMIC-ADDRESS] => -249310
)
foreach($addresses as $param => $value){
//$param would be DYNAMIC-ADDRESS in a specific "round"
//$value would be -249310
}
I would consider to change the format of the returned data's array, unless its a 3rd party API - there's no reason you should work with unknown key's names.
This is the decoded array which i'm getting from url using php curl method,
Array ( [result] => Array ( [0] => Array ( [id] => 1 [name] => FIRDOUS FAROOQ BHAT [office] => MG Road [age] => 25 [start_date] => 2017-04-27 22:08:11 [salary] => $20000 ) ) )
Now the problem is i'm not able to fetch a particular value from it.I used echo $result->name; as well as var_dump['name'];,i'm getting null value. Can anyone sort it out?
if your variable name is $data where you are storing your this array,
echo $data['result'][0]['name'];
echo $data['result'][0]['office'];
or (if multiple data)
foreach($data['result'] as $res){
echo $res['name'];
echo $res['office']; //if office there
echo $res['age'];
}
You decode you json string into array, you need to use index to access array element like $result['result'][0]['name'];. You cannot use -> to access array element, this operator is used to access element of an object.
If the output you've posted here is stored in $result, you would want to access it as such:
//Get the first result, and the name from that first result
$result['result'][0]['name'];
Hello Here if result contains more than one element in array. In this case safe way to access your result is. And Here I am considering your response from CURL you will store inside $result variable if you will do it like this then below code will helps you.
foreach($result['result'] as $singleArray)
{
echo $singleArray['name'];
}
Like this you can access all elements of result array.
Here you are getting an array but you are tried to access the object,
echo $result->name;
You shouldn't use this instead use this
echo $data['result'][0]['name'];
I've experienced a problem when developing for a minecraft server: I have an array like this which is received as json and encoded but for processing the output of this I need to know how to access the array and echo one of the values for Example the Version Key.I tried to save the array as variable $json and to access it via echo $json->Version; but this results in the output of this error: Notice: Trying to get property of non-object in /home/bs-web/joomla/api/test2.php on line 12
HereĀ“s the Array:
Array
(
[0] => stdClass Object
(
[GameName] => MINECRAFT
[Version] => 1.8
[Plugins] =>
[Map] => BungeeCord_Proxy
[Players] => 7
[MaxPlayers] => 100
[HostPort] => 25565
[HostIp] => 188.40.97.86
[Software] => Vanilla
)
)
Your variable $json is an array, you cannot get property of an array. Assuming you have only one element in the array as in your example, you should use
echo $json[0]->Version;
to display the version(1.8 in your example).
once you saves the $json variable as an array, you only can access its values passing the key that you want to use, and then, point to the object. In your case, to get the Version property, you will need to use $json[0]->Version.
To access and echo all values of your object you can use a foreach loop to iterate with it.
foreach ($json as $object){
echo $object->GameName;
echo $object->Version;
echo $object->Plugins;
.... // All properties that you want
}
Hope it helps you, hugs.
You could also use json_decode($json, true) then you get an assocative array which you can access like this $json['Version']
I'm trying to store an array ($temp) into the $data array, where key is prices.
$data['prices'] = $temp;
However, PHP converts the array into string instead and is throwing me and error.
Notice: Array to string conversion
Is $data = array('prices' => $temp); the only solution?
edit:
I found my mistake. Yes, $data was used previously as a string that's why PHP is converting the input into string.
Problem 2, doing a print_r on $data['prices'] = $xml->result->Prices->Price, shows only 1 set of array. But I am able to retrive 2 sets of result by doing a foreach loop on $data['prices']. Why is that so?
Content of $temp http://pastebin.com/ZrmnKUPB
Let me be more clear..
The full xml object I'm trying to extract information from: http://pastebin.com/AuMJiyrw
I'm only interested in the price array (Price_strCode and Price_strDescription) and store them in $data['prices']. End result something like this:
Array(
[0] => (
[Price_strCode] => 0001
[Price_strDescription] => Gold
)
[1] => (
[Price_strCode] => 0002
[Price_strDescription] => Silver
)
)
Unless you are doing some other array to string conversion elsewhere, the array is actually being stored as another array.
$data['prices'] will be an array, which can be accessed as $data['prices']['key'].
This is not possible, I have been doing this always and it works fine. You must be doing something else somewhere which is causing your array to convert to strong. share your code here