1

I have complex json data. It can be in either formats as below :

1. {"Physics":{"5b87ceb691":"Motion","5b87ce3e":"Mass"}} //where value itself is a json
2. {"a60a8001805":"Finish chapter 1","a60a8002ab0":"Finish assignments"} //simple key value pair

I want to create checkboxes for all values (Motion, Mass, Finish chapter 1, Finish assignments for above examples) and keys should come like text in between those checkboxes (Physics for above example) (Key should come like text only if it's value is a json object, since Physics's value is a json, Physics should show like a text and in example 2, since there are no such keys whose value is a json object, no key will show like a text).

I've implemented this in javascript in the following way :

var str = "";
if(Object.keys(data).length !== 0) {
var i = 1;
for (var key in data) {
  if (data.hasOwnProperty(key)) {
    var val = data[key];
    if(typeof val == 'object') {
      str += '<div class = "data_text hidden">' + key + '</div>';
      for (var subKey in val) {
        if (val.hasOwnProperty(subKey)) {
          var subValue = val[subKey];
          str += '<div class="db-checkbox data_options hidden"><input type="checkbox" name="abc" id="' + i + '"><label for="' + i + '">' + subValue + '</label></div>'
          i += 1;
        }
      }
   } else {
    str += '<div class="db-checkbox data_options hidden"><input type="checkbox" name="abc" id="' + i + '"><label for="' + i + '">' + val + '</label></div>'
    i += 1;
   }
  }
}

Now, I want to do the same in php. I searched to find out the way to access json data in php, but all of those works only when keys are known. (For example, data->a60a8002ab0) But I want to do this generically. Don't want to hardcode anything. How can I do this? Thank you.

Neha
  • 25
  • 4
  • start with [json_decode](https://www.php.net/manual/en/function.json-decode.php) and write code from there – Bravo Jul 23 '21 at 04:02
  • 1
    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) – ADyson Jul 23 '21 at 06:34

3 Answers3

3

Try json_decode in associative mode by passing in the second param true so that it returns an associative array that you can loop through, similar to your javascript code.

Hisham
  • 411
  • 3
  • 9
1
//try each of the following by comment/uncomment
//$myjson = '{"Physics":{"5b87ceb691":"Motion","5b87ce3e":"Mass"}}';
//$myjson = '{"a60a8001805":"Finish chapter 1","a60a8002ab0":"Finish assignments"}';
$myjson = '{"a60a8001805":"Finish chapter 1","a60a8002ab0":"Finish assignments","Physics":{"5b87ceb691":"Motion","5b87ce3e":"Mass"}}';

$json_md_ary = json_decode($myjson,true);
$i=1;
foreach($json_md_ary as $key=>$val){
    if (is_array($val)){
        echo $key, "<br>";
        foreach($val as $k=>$v){
            echo $k, ' => ' ,$v,'<input type="checkbox" name="abc" id="'.$i.'">',"<br>";
            $i++;
        }
    }else{
        echo $key, ' => ' ,$val,'<input type="checkbox" name="abc" id="'.$i.'">',"<br>";
        $i++;
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
GUEST
  • 11
  • 1
0
$jsondata = '{"Physics":{"5b87ceb691":"Motion","5b87ce3e":"Mass"}}';

$data = json_decode($jsondata);
 
foreach($data as $key => $val){
   print_r($val);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135