So I have this array that saves a json string.
Array
(
[0] => "2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}
)
I want to be able to access just the value "2jDQoU9D2wu04wqkg0ImUI" at the start of the json string and then also be able to do something like $value[0]['type'] to get the type from this json string object. I'm pretty new to PHP and struggling to get this working. I've tried JSON encoding/decoding and can't seem to get anything to work.
What's the proper way to go about this? Thanks in advance.
I hope this code will solve your problem.
$array[0] = '"2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}';
//print_r($arr);
$JsonString = '{' . $array[0] . '}';
$json = json_decode($JsonString);
foreach($json as $key => $value){
echo "Key : $key <br />";
echo "Type : ". $value->type."<br />";
echo "date : ". $value->date."<br />";
echo "story_id : ". $value->story_id."<br />";
echo "series_id : ". $value->series_id."<br />";
}
so you have array $jsonArray and you want to access just the key of it
if its single dimension array :
echo key($jsonArray); //prints 2jDQoU9D2wu04wqkg0ImUI
Otherwise if its multidimensional you can loop through it and do whatever you want with each key
foreach($jsonArray as $key => $value) {
echo $key; //prints 2jDQoU9D2wu04wqkg0ImUI
}
Try this:
$arrayOfObjects = [];
foreach($array as $key => $value) arrayOfObjects[] = json_decode($value);
Now you can loop through this new $arrayOfObjects
foreach($arrayOfObjects as $key => $object){
echo $object->date . '<br>';
echo $object->type . '<br>';
echo $object->story_id . '<br>';
echo $object->series_id . '<br> ==== >br> ';
}
Related
this is my code, I want to echo all the data inside this $json.
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json,true);
foreach($arr as $key=>$value){
echo $key . "<br>";
echo $value . "<br>"; // PHP Notice: Array to string conversion in /workspace/Main.php on line 11
// not displaying the value
}
?>
the problem come went the data loop on session_from_at which have three data in array inside.
I think you are not using blade. Your file name is Main.php
If you are using blade, I advise you to use #foreach instead of foreach.
And after json_decode, you still have an array inside array.
Try this code:
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json, true);
function echoArray($arr) {
foreach($arr as $key=>$value){
echo $key . "<br>";
if (is_array($value)) {
echoArray($value);
} else {
echo $value . "<br>";
}
}
}
echoArr($arr);
You got an error because other $value is an array you cant echo it so try to check if its array or object like this :
foreach($arr as $key=>$value){
echo $key . "<br>";
if(is_object($value) || is_array($value)){
foreach($value as $key2=>$value2){
echo $key2 . "<br>";
echo $value2 . "<br>";
}
}else{
echo $value . "<br>";
}
}
try it:
$arr['year'];
$arr['month'];
you do not need to use foreach.
So I would like to create just one loop to parse the json data i have. I can successfully parse it in 2 foreach loops however when trying to combine in one loop using $key => $value the $key returns nothing when called. How can I successfully take the 2 foreach loops I have here and combine them into one?
$contents = file_get_contents($url);
$results = json_decode($contents, true);
$jsonList = $results['genres'];
foreach($jsonList as $key) {
$GenreID = $key['id'].'<br>';
echo $GenreID;
}
foreach($jsonList as $key => $value) {
$GenreName = $value['name'].'<br><br>';
echo $GenreName;
}
The json data is as follows:
{"genres":[{"id":28,"name":"Action"},{"id":12,"name":"Adventure"},{"id":16,"name":"Animation"},{"id":35,"name":"Comedy"},{"id":80,"name":"Crime"},{"id":99,"name":"Documentary"},{"id":18,"name":"Drama"},{"id":10751,"name":"Family"},{"id":14,"name":"Fantasy"},{"id":36,"name":"History"},{"id":27,"name":"Horror"},{"id":10402,"name":"Music"},{"id":9648,"name":"Mystery"},{"id":10749,"name":"Romance"},{"id":878,"name":"Science Fiction"},{"id":10770,"name":"TV Movie"},{"id":53,"name":"Thriller"},{"id":10752,"name":"War"},{"id":37,"name":"Western"}]}
You can still use $key as an index when also extracting the $value.
However note that you shouldn't be assigning the line breaks to your variables, and should instead consider them part of the view by echoing them out independently:
$contents = file_get_contents($url);
$results = json_decode($contents, true);
$jsonList = $results['genres'];
foreach($jsonList as $key => $value) {
$GenreID = $key['id']; // Depending on structure, you may need $value['id'];
$GenreName = $value['name'];
echo $GenreID . '<br>';
echo $GenreName . '<br><br>';
}
Your single loop below here.
foreach($jsonList as $key)
{
$GenreID = $key['id'].'<br>';
echo $GenreID;
$GenreName = $value['name'].'<br><br>';
echo $GenreName;
}
$key is a assosative array.Therefore it had some index.So you can use this index in single loop.
It seems you get confused over your data structure. Seeing the json, the resulting "$jsonlist" supposed to contain array with id and value as keys.
You can iterate over it and extract the the appropriate key.
Myabe something like this:
foreach($jsonlist as $value) {
echo "id: " . $value['id'] . "\n";
echo "name: " . $value['name'] . "\n";
}
extra bonus, if you want to create 1 level array from your json based on id with the name you could try array reduce using anon function like this:
$jsonlist = array_reduce($jsonlist, function($result, $item){
$result[$item['id']] = $item['name'];
return $result;
}, []);
extra neat for transformation of static structure data.
I am trying to get user profile from pinterest using oauth api.
code for user data :
$me = $pinterest->users->me(array(
'fields' => 'username,first_name,last_name,image[large]'
));
and echo result by :
echo $me;
the output is as follow :
{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}
Now i want to echo this result as
id="195414208739840616"
username="rajivsharma033"
first_name="Rajiv"
and so on...
please help me.
Since you are getting json data so you need to use json_decode():-
<?php
$me = '{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}';
$array_data = json_decode($me);
echo "<pre/>";print_r($array_data);
foreach ($array_data as $key=>$value){
if($key == 'image'){
echo $key. " url is=" . $value->large->url .'<br/>';
}else{
echo $key. "=" . $value .'<br/>';
}
}
I would solve it by using two times foreach:
$a = json_decode($me);
foreach ($a as $key=>$value){
echo $key.'="'.$value.'"<br/>';
}
foreach ($a['image']['large'] as $key=>value){
echo 'image-large-'$key.'="'.$value.'"<br/>';
}
Alternatively, you can do this recursive:
function echojson($string=''){
$a = json_decode($me);
foreach ($a as $key=>$value){
if (is_array($value)) echojson($string.'-'.$key);
else
echo $string.$key.'="'.$value.'"<br/>';
}
}
Thanks for your time in reading this post.
My php file is receiving a json object. But I am facing issues while decoding it.
My php code:
$data=$_POST['arg1'];
echo $data;
$json = json_decode($data,true);
echo $json;
$i = 1;
foreach($json as $key => $value) {
print "<h3>Name".$i." : " . $value . "</h3>";
$i++;
}
When I echo data results as below.
{
"SCI-2": {
"quantity": 2,
"id": "SCI-2",
"price": 280,
"cid": "ARTCOTSB"
}
}
When I echo $json, result is as it follows :
Array
Name1 : Array.
Please assist as i need tho access the cid and quantity values in the $data.
json_decode returns an array. And to print array you can use print_r or var_dump.
Now to access your values you can try :
$json["SCI-2"]["quantity"] for quantity and $json["SCI-2"]["cid"] for cid.
Demo : https://eval.in/522350
To access in foreach you need this :
foreach($json as $k) {
foreach($k as $key => $value) {
print "<h3>Name".$i." : " . $value . "</h3>";
}
}
Since you do not know the number of items in your object, use this:
$obj = json_decode($json);
After this, iterate the $obj variable and after that, inside the loop, use the foreach to get each property.
foreach($iteratedObject as $key => $value) {
//your stuff
}
I have an array $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);. How can i get (echo) all names and corresponding ages in a single instance (like foreach $value as $value)?? The array may have more data than shown here.
foreach ($ages as $name => $age) {
echo "$name = $age\n";
}
u have a lot options
like
print_r
print_r($array)
var_dump , foreach
print_r($array);
or
var_dump($array)
foreach ($ages as $key => $value) {
echo $key . "'s age is " . $value . "<br />";
}