0

I want to retrieve daily activity data

The json form looks like this:

{ 
"results": { 
"todo": [ 
"Buy 1 Macaroni", 
"Eat", 
"Praying", 
"Sleep" 
]
} 

Then I decode the json.

The question is how can I retrieve the key from "todo"

I want to encode the json and print it using php

When i use

$ing = '{ 
"results": { 
"todo": [ 
"Buy 1 Macaroni", 
"Eat", 
"Praying", 
"Sleep" 
]
}';
foreach ($ing as $ingre){
echo $ingre;
}

It's only print 1 line

Herojan
  • 11
  • 1
  • What exact output did you want? – ADyson Feb 16 '21 at 16:46
  • Print "todo" with pbp – Herojan Feb 16 '21 at 16:47
  • 1
    Iterate and output each value? – El_Vanja Feb 16 '21 at 16:55
  • 3
    Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – El_Vanja Feb 16 '21 at 16:55
  • Once you decide the JSON, Todo is an array. It can't be printed directly to the page. So how you do you want it to appear in the page? You want it back in JSON format, or in a html table, or something else? You want the data in the same order? There are lots of possibilities. That's why we asked you to tell us what **exactly** you want – ADyson Feb 16 '21 at 17:01
  • Yes, I mean, I want to print a "todo" list example: Buy 1 Macaroni, Eat, Praying, Sleep – Herojan Feb 16 '21 at 17:07

2 Answers2

0

It's pretty straightforward to convert JSON to arrays in PHP. But first, your JSON file is missing a curly brace. Also, there is a process of converting JSON string to PHP usable datatypes. The first thing is to decode the JSON string using json_decode(). This function returns an appropriate PHP type, in this case, an object. To access an object's data you use -> as shown in the code

  "results":{
   "todo":[
      "Buy 1 Macaroni",
      "Eat",
      "Praying",
      "Sleep"
   ]
   }
 }

To convert this, you can simply do :

    $json = '{
     "results":{
      "todo":[
         "Buy 1 Macaroni",
         "Eat",
         "Praying",
         "Sleep"
      ]
       }
     }';
     $todos = json_decode($json);
     var_dump($todos->results->todo);

The output is :

array(4) {
[0]=>
string(14) "Buy 1 Macaroni"
[1]=>
string(3) "Eat"
[2]=>
string(7) "Praying"
[3]=>
string(5) "Sleep"

If you'd like to access each element of an array using a loop instead of var_dump, then do the following

$json = '{
 "results":{
  "todo":[
     "Buy 1 Macaroni",
     "Eat",
     "Praying",
     "Sleep"
     ]
   }
 }';
 $todos = json_decode($json);
 
 foreach($todos->results->todo as $todo )
 {
     echo $todo."\n";
 }

Output is

Buy 1 Macaroni
Eat
Praying
Sleep
  • Hi thanks for answering, when I use foreach, why its only print 1 line (Sleep) – Herojan Feb 16 '21 at 17:53
  • @Herojan then either you didn't copy this example correctly, or your data doesn't contain what you thought it does – ADyson Feb 16 '21 at 19:00
  • @Herojan you are missing a closing curly brace (```}```) at the end. Right before foreach you need a curly brace. Above your for each statement, change it to look like ```}}';``` – Daniel Gitahi Feb 16 '21 at 19:25
0

you can get the 'todo' value from the json by specifying the key in array_column and with the json_decode

<?php
$json='{ 
"results": { 
"todo": [ 
"Buy 1 Macaroni", 
"Eat", 
"Praying", 
"Sleep" 
]
} ';
$myJSON =array_column( json_decode($json), 'todo');
echo $myJSON;
?>
Albertk14
  • 1
  • 2