dongzong1866 2016-02-06 12:39
浏览 51
已采纳

Json的三维关联数组?

After several attempts, I have to ask for some help to solve this. Below I have some plain JSON:

{
    "test": {
        "r1": [{
            "id": 1,
            "status": true
        }, {
            "id": 2,
            "status": true
        }],
        "r2": [{
            "id": 1,
            "status": false
        }, {
            "id": 2,
            "status": false
        }]
    }
}

This works fine with javascript when I'm reading from a simple .txt file, but I want to create this JSON from PHP. I can make a two dimensional associative array, but for this it seems that I need a three dimensional associative array, and I can't solve that! Could someone give me a hint how that would look or alternative solutions?

  • 写回答

1条回答 默认 最新

  • doujuan2688 2016-02-06 13:07
    关注

    Here's an embracing example with your data on how to convert from JSON object (string) to PHP array and vice-versa.
    Hope this makes things clear for you.

    <?php
    
    // Original JSON object string
    $jsonstring = '{
        "test":{
            "r1":[{
                "id":1,
                "status":true
            },{
                "id":2,
                "status":true
            }],
            "r2":[{
                "id":1,
                "status":false
            },{
                "id":2,
                "status":false
            }]
        }
    }';
    
    // Convert JSON string to PHP array
    // This can be used by a PHP script to work on
    $phparray = json_decode($jsonstring);
    echo '<h3>PHP array converted from JSON string</h3><pre>'; var_dump($phparray); echo '</pre>';
    
    // Convert it back to JSON string to prove it's the same
    $jsonstring1 = json_encode($phparray);
    
    // Now we create a PHP array corresponding to original JSON string, manually
    $phparray = array (
        "test"=> array (
            "r1" => array(
                array(
                    "id"=>1,
                    "status"=>true
                ),
                array(
                    "id"=>2,
                    "status"=>true
                )
            ),
            "r2" => array(
                array(
                    "id"=>1,
                    "status"=>false
                ),
                array(
                    "id"=>2,
                    "status"=>false
                )
            )
        )
    );
    
    // Convert PHP array to JSON string
    // This can be sent to a browser where it can be used by Javascript
    $jsonstring2 = json_encode($phparray);
    
    echo '<h3>Original JSON string</h3>' . $jsonstring;
    echo '<h3>After conversion to array and back</h3>' . $jsonstring1;
    echo '<h3>Converted from PHP array</h3>' . $jsonstring2;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀