这是一个反向工程问题,但我想知道如何用PHP编写一个适当的多维数组,该数组输出以下javascript数组。
[
{
"key": "Basic Planners",
"values": [{"x": "YourPhone","y": 150},
{"x": "Universe X3","y": 300},
{"x": "ePhone 74s","y": 1500},
{"x": "NextUs","y": 50},
{"x": "Humanoid","y": 500
}]
}, {
"key": "No-Namers",
"values": [{"x": "YourPhone","y": 300},
{"x": "Universe X3","y": 250},
{"x": "ePhone 74s","y": 400},
{"x": "NextUs","y": 150},
{"x": "Humanoid","y": 900}]
}, {
"key": "Feature Followers",
"values": [{"x": "YourPhone","y": 350},
{"x": "Universe X3","y": 900},
{"x": "ePhone 74s","y": 100},
{"x": "NextUs","y": 500},
{"x": "Humanoid","y": 250}]
}, {
"key": "Hipsters & Elites",
"values": [{"x": "YourPhone","y": 200},
{"x": "Universe X3","y": 350},
{"x": "ePhone 74s","y": 50},
{"x": "NextUs","y": 800},
{"x": "Humanoid","y": 100}]
}
]发布于 2014-07-05 02:39:27
下面的代码应该能做到这一点
$phpArray = array(
array(
'key' => 'Basic Planners',
'values'=> array(
array('x' => 'YourPhone', 'y' => 150),
array('x' => 'Universe X3', 'y' => 300),
array('x' => 'ePhone 74s', 'y' => 1500),
array('x' => 'NextUs', 'y' => 50),
array('x' => 'Humanoid', 'y' => 500),
)
),
/* and so on... */
);
echo json_encode($phpArray);发布于 2014-07-05 02:36:19
对于JSON对象,请使用array("key" => value, ...)
对于JSON数组,请使用array(arg0, arg1, arg2, ...)
然后把这些不同的组合嵌套起来。这将输出所需的JSON。
https://stackoverflow.com/questions/24582396
复制相似问题