【问题标题】:PHP - JSON to Multidimensional Array From JQuery NestablePHP - 从 JQuery Nestable 到多维数组的 JSON
【发布时间】:2016-02-22 07:11:40
【问题描述】:

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

【问题讨论】:

  • 你没有听说过 php 函数:json_encode, json_decode
  • 对于函数json_encode,我明白了。但是在我使用 json_decode. 之后,我对嵌套数组感到困惑

标签: php arrays json multidimensional-array


【解决方案1】:

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 的第二个参数使其成为数组而不是对象。

【讨论】:

  • 还是谢谢先生..但我对嵌套数组感到困惑。我想转换成一个数组,然后保存到mysql。
  • 它不再比你的 json 嵌套。你的 json 嵌套越多,你的数组就越嵌套。我想我的回答回答了你的问题。 Json_decode 会将 JSON 转换为 Array。
【解决方案2】:

谢谢大家...我已经尝试并且可以解决我的问题。我的代码:

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

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    相关资源
    最近更新 更多