Say Pulung Say Pulung - 5 months ago 57
JSON Question

PHP - JSON to Multidimensional Array From JQuery Nestable

how to converting JSON data to array. i have data in JSON like :

[{
"id":11,
"children":[
{
"id":12,
"children":[
{
"id":13
}]
}]
},
{
"id":14,
"children":[
{
"id":15,
"children":[
{
"id":16
}]
}]
},
{
"id":18
}]


I want to converting to

Array(
[0] Array(
[id]=>11,
[parent_id]=>0),
[1] Array(
[id]=>12,
[parent_id]=11),
[2] Array(
[id]=>13,
[parent_id]=>12),
[3] Array(
[id]=>14,
[parent_id]=>0
)....etc
)


I use CodeIgniter3. I try this but not work.

function find_parent($array, $needle, $parent = null) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$pass = $parent;
if (is_string($key)) {
$pass = $key;
}
$found = $this->find_parent($value, $needle, $pass);
if ($found !== false) {
return $found;
}
} else if ($key === 'id' && $value === $needle) {
return $parent;
}
}
}



public function save_menu(){
$data = $this->input->post('data');
echo $data;
$dd = json_decode($data,TRUE);

echo '<br>';
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($dd));
foreach($it as $v) {
$parent = 0;
$parent = $this->find_parent($dd,$v);
echo $v, " ".$parent.'<br>';
}
}





I have try this, but I can't get the parent_id of array

function find_parent($array, $needle, $parent = null) {
$ff = '';
$par = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$pass = $parent;
//echo 'pass 1 = '.$pass.'<br>';
if (is_string($key)) {
$pass = $key;
//echo 'pass 2 = '.$pass.'<br>';
}
$found = $this->find_parent($value, $needle, $pass);
if ($found !== false) {
return $value;
//return $value['id'];
}
} else if ($key === 'id' && $value === $needle) {
//echo 'parent = '.$parent.' '.$value['id'].'<br>';
return $parent;
//$par = $parent;
}
}
//return $ff;
return false;
}
public function save_menu(){
$data = $this->input->post('data');
echo $data;
$dd = json_decode($data,TRUE);

echo '<br>';
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($dd));

foreach($it as $v) {
$parent_id='';
$parent = 0;
$parent = $this->find_parent($dd,$v);
foreach ($parent as $key => $value) {
if(is_array($value)==FALSE){
//echo $value;
$parent_id = $value;
break;
}
}
$tp = (($parent_id==$v)?0:$parent_id);
//$this->db->where('id_menu_content',$v);
//$this->db->update('gink_menu_contents',array('parent_id'=>$tp));
echo $v, " ".$tp.'<br>';
}
//redirect('gink/menu_editor');
}

Answer

Thanks all... I have try and can be solve my problem. My code :

$d = '[{"id":11,"children":
    [{"id":12,"children":
    [{"id":13,"children":
    [{"id":14,"children":
    [{"id":15}]}]}]}]},
    {"id":18}]';
$data = json_decode($d , TRUE);
echo '<br>';
$hasil_simpan = array();
function print_rec($d=array(),$parent = 0){
    $iterator = new RecursiveArrayIterator($d);
    while($iterator->valid()){
        if($iterator->hasChildren()){
            $last_parent = 0;
            foreach ($iterator->getChildren() as $key => $value) {
                if(is_array($value)){
                    print_rec($value,$last_parent);
                }else{
                    $last_parent = $value;
                    echo "id : ".$value ." ; parent_id : ".$parent." ". "<br>";
                }
            }
        } else {
            echo "No children.\n";
        }

        $iterator->next();
    }
}

echo '<br>'.print_rec($data);
Comments