[英]How to create this array structure in PHP
I am trying to create an array structure like the initial one found here: D3 JSON data conversion 我正在尝试创建一种数组结构,类似于在此处找到的初始结构: D3 JSON数据转换
Mine would look something like the below, be dynamic, and then be passed to json_encode() to make a json object. 我的看起来像下面这样,是动态的,然后被传递到json_encode()以创建一个json对象。
[
{ "name": "Table1", "lab": "name1", "cell": "c1", "avg": avgUsage1},
{ "name": "Table1", "lab": "name2", "cell": "c2", "avg": avgUsage2},
{ "name": "Table1", "lab": "name3", "cell": "c3", "avg": avgUsage3},
{ "name": "Table1", "lab": "name4", "cell": "c4", "avg": avgUsage4},
{ "name": "Table1", "lab": "name5", "cell": "c5", "avg": avgUsage5} ...
]
The problem is that I cannot have an array that has the same values for the key. 问题是我不能拥有键值相同的数组。 What is this data structure and how can I create something like it to then create a nested structure (I will follow the linked post to create the nested structure)? 这个数据结构是什么?如何创建类似的数据然后创建嵌套结构(我将按照链接的文章来创建嵌套结构)?
Not sure why you think there would be same keys. 不知道为什么您会认为会有相同的键。 You would have a multidimensional array in php that would look like: 您将在php中创建一个多维数组,如下所示:
$arr = array(
array( "name"=> "Table1", "lab"=> "name1", "cell"=> "c1", "avg"=>$avgUsage1),
array( "name"=> "Table2", "lab"=> "name2", "cell"=> "c2", "avg"=>$avgUsage2),
//....
);
or each element could also be a stdClass
object 或者每个元素也可以是stdClass
对象
You can initialize it like that: 您可以像这样初始化它:
$arrayJSON = array(
array(
"name" => "Table1",
"lab" => "name1",
"cell" => "c1",
"avg" => "avgUsage1"
),
array(
....
)
);
You can also make an array of class instances. 您还可以创建一个类实例数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.