如何多次多次创建嵌套数组数组的JSON对象?

时间:2013-08-24 06:25:14

标签: php

$data=array
(
    "Maths" : [ 
        {
            "Name"  : "ramesh",        // First element
            "Marks" : 67,
            "age"   : 23
        }, {
            "Name"  : "mayur",    // Second element
            "Marks" : 65,
            "age"   : 21
        }
    ], 
"Science" : [
        {
            "Name"  : "ram",     // First Element
            "Marks" : 56,
            "age"   : 27
        },
        {
            "Name"  : "Santosh",    // Second Element
            "Marks" : 78,
            "age"   : 41
        }
   ]
);

1 个答案:

答案 0 :(得分:2)

您可以创建$data并像这样编码json

<?php
$data = array
(
    "Maths" => array(
        array(
            "Name" => "ramesh", // First element
            "Marks" => 67,
            "age" => 23
        ), array(
            "Name" => "mayur", // Second element
            "Marks" => 65,
            "age" => 21
        )
    ),
    "Science" => array(
        array(
            "Name" => "ram", // First Element
            "Marks" => 56,
            "age" => 27
        ),
        array(
            "Name" => "Santosh", // Second Element
            "Marks" => 78,
            "age" => 41
        )
    )
);
echo json_encode($data);