PHP - 来自JQuery Nestable的多维数组的JSON

时间:2016-02-22 07:11:41

标签: php arrays json multidimensional-array

如何将JSON数据转换为数组。我有JSON数据,如:

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

我想转换为

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
    )

我使用CodeIgniter3。我试试这个但不行。

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>';
    }
}

我试过这个,但是我无法获得数组的parent_id

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');
}

2 个答案:

答案 0 :(得分:0)

PHP确实有json_encode()json_decode()

所以使用:

$json = "[{ "id":11, "children":[ { "id":12, "children":[ { "id":13 }] }] }, { "id":14, "children":[ { "id":15, "children":[ { "id":16 }] }] }, { "id":18 }]";
$decoded = json_decode($json, true);

TRUE作为json_decode的第二个参数使它成为一个数组,而不是对象。

答案 1 :(得分:0)

谢谢大家......我试过,可以解决我的问题。我的代码:

$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);