Get name of a random array from a JSON parent array - php

I have some JSON like this:
{"100":{"id":100,"name":"example"},"200":{"id":200,"name":"example2"}}
Which I converted to an array:
[100] => Array
(
[id] => 100
[name] => "example"
)
[200] => Array
(
[id] => 200
[name] => "example"
)
I want to return a random [id] - or a random array name because they are the same as the IDs. How can I do this?
I've tried:
$src = "http://domain.com/json.json";
$data = file_get_contents($src);
$obj = json_decode($data, true);
$rand = array_rand($obj);
echo strval($obj[$rand]);
Which outputs:
Notice: Array to string conversion in C:/Users/Will/Documents/PHP/TestJson.php on line 7
Array
I want it to either output 100 or 200.

$jsonArr = json_decode($str, TRUE);
$rand_array_key = array_rand($jsonArr);
echo $jsonArr[$rand_array_key]['id'];
Demo

Related

How do I loop through using foreach for a json decoded array

Array
(
[id] => 7.8803211967429E+17
[title] => Example T-Shirt
[body_html] =>
[variants] => Array
(
[0] => Array
(
[id] => 6.4266704147271E+17
)
[1] => Array
(
[id] => 7.576504846442E+17
[product_id] => 7.8803211967429E+17
)
)
[options] => Array
(
[0] => Array
(
)
)
)
I need to get IDs from each main array. I get this array from decoding json using:
<?php
set_time_limit(0);
$json = file_get_contents('imarasoft.net/imarasoft/abd/WebHook/response.txt');
$arrays = json_decode($json, true);
print_r($arrays);
foreach ($arrays as $array) {
echo $array["id"];
}
How can I print ID using foreach?
i HAVE EDITED THE QUESTION AND HAVE GIVEN THE FULL ARRAY FORMAT
If you are getting more number of elements in the array then you can use below snippets. This foreach loop go through all the arrays and gives value of each array's key. Here you can any value like title, id, vendor, product_type etc...
If you want access id inside variants then use below code.
$json = file_get_contents('http://imarasoft.net/imarasoft/abd/WebHook/response.txt');
$arrays = json_decode($json, true);
foreach ($arrays["variants"] as $array) {
echo $array["id"];
}
And if you only want to access root id then you just need to do only one line snippet.
echo $arrays["id"];

get value from array - google API PHP result

I am trying to get the driving distance from google maps.
print_r($result); outputs the below:
Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 3,936 km
[value] => 3935862
)
[duration] => Array
(
[text] => 1 day 18 hours
[value] => 150860
)
[status] => OK
)
)
)
)
my php code to produce this is:
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Sydney&destinations=Perth&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
$details=$details['rows'];
print_r($details);
I somehow need to access this array and get the distance text value, in this case 3936 km
Any help appreciated,
Thanks as always
<?php
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Sydney&destinations=Perth&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
$details=$details['rows'][0]['elements'][0]['distance']['text'];
print_r($details);
?>
there you go
If we assume that variable $array holds the array you have attached to your post then we can do the following:
echo $array['elements'][0]['distance']['text']; //should output 1300km
Hope it helps!

Getting arraylist of json

I have the following Json
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => Array
(
[0] => 230
[1] => 1956
[2] => 1958
[3] => 2294
)
How do i get the data array out of the response?
I know this is quite simple.
update
Here is some of my source code
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';
$data = $result->response->data;
print_r($data);
$json_object = json_decode($result);
print_r($json_object->response->data);
In PHP, -> is the object operator (or arrow). I'd encourage you to read more about Objects in PHP and json_decode().
That is not JSON, that is a PHP array or object. You didn't provide enough info to tell which one it is.
You can access the data array from it using either:
$data = $arr['response']->data;
Or:
$data = $obj->response->data;
Replace $arr or $obj with the actual variable name.
Edit
Your variable contains a string, because after decoding it you did not save the result. Try the following code:
$url = $base . http_build_query( $params );
$json = file_get_contents( $url );
$result = json_decode($json);
$data = $result->response->data;
echo '<pre>',print_r($data, true),'</pre>';
simply like this :-
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => Array
(
[0] => 230
[1] => 1956
[2] => 1958
[3] => 2294
)
$json_data=json_decode($response,true);

Retrieve name-value pair into PHP array

I need some help. I have a variable containing this string;
[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]
I need to retrieve the id and value for each pair and make it any array in PHP.
You can use json_decode and pass the second param as true so it returns an array like this
$json = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$decoded = json_decode($json,true);
print_r($decoded);
Working Example
Output would be
Array
(
[0] => Array
(
[id] => 17
[value] => 123456789
)
[1] => Array
(
[id] => 18
[value] => 2012-06-13
)
[2] => Array
(
[id] => 19
[value] => Kampala
)
[3] => Array
(
[id] => 20
[value] => 1
)
.......
)
Which you can loop through using foreach like.
foreach($decoded as $de){
// access id with $de['id']
// access value with $de['value']
}
You have got an json string. You can convert it to an array by using function json_decode
Check this code .
$str = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"}, {"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$array = json_decode($str);
foreach($array as $temp){
echo "ID : ".$temp->id."\t Value: ".$temp->value;
echo "<br />";
}

Why doesn't this work?: decoding json into php array

$json = file_get_contents('outputsjson.php');
The file encodes an array then just echoes it as this (and echo $json outputs this):
{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}}
Now I'm trying to decode it from another page like this:
$myarray = json_decode($json, true);
print_r($myarray);
This outputs nothing, no errors, nothing!
Try this instead (you are mixing " and ' [single quotes instead of double quotes on the string]):
$json = '{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}} ';
$myarray = json_decode($json, true);
print_r($myarray);
And your result:
Array
(
[theList] => Array
(
[1] => Array
(
[name] => DSC04156.JPG
[title] => DSC04156.JPG
[width] => 3264
)
[2] => Array
(
[name] => DSC04157.JPG
[title] => DSC04157.JPG
[width] => 3264
)
[3] => Array
(
[name] => DSC04158.JPG
[title] => DSC04158.JPG
[width] => 3264
)
[4] => Array
(
[name] => DSC04159.JPG
[title] => DSC04159.JPG
[width] => 3264
)
)
)
Try wrapping the json string in single quotes instead of double quotes:
$json = '{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}}';
I had no problems executing the following code:
$json = '{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}}';
$myarray = json_decode($json, true);
print_r($myarray);
My guess would be that the file you are trying to read from does not exist. Remember that, if you are using Linux, file names are case-sensitive. Use the file_exists() function to check this:
var_dump(file_exists('outputsjson.php'));

Categories