Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

622
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda