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 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决