Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

623
Views
How to access json data in php?

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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

//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++;
    }
}
over 4 years ago · Santiago Trujillo Report

0

$jsondata = '{"Physics":{"5b87ceb691":"Motion","5b87ce3e":"Mass"}}';

$data = json_decode($jsondata);
 
foreach($data as $key => $val){
   print_r($val);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!