Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中创建具有嵌套对象和数组的JSON对象_Php_Arrays_Json_Nested - Fatal编程技术网

如何在php中创建具有嵌套对象和数组的JSON对象

如何在php中创建具有嵌套对象和数组的JSON对象,php,arrays,json,nested,Php,Arrays,Json,Nested,这就是我希望对象的外观: { "cluster": "testcluster", "instance": "i-03f8f8c9e7febab21", "instype": "r4.8xlarge", "AZ": "us-east-1e", "env": "test", "cpus": "32", "memory": "256G", "kernel": "4.4.0-96-generic", "info": "cpu u

这就是我希望对象的外观:

{
    "cluster": "testcluster",
    "instance": "i-03f8f8c9e7febab21",
    "instype": "r4.8xlarge",
    "AZ": "us-east-1e",
    "env": "test", 
    "cpus":  "32",
    "memory": "256G",
    "kernel": "4.4.0-96-generic",
    "info": "cpu usage stats",
    "unit": "percent",
    "cpustats": [
          {"metric":"user", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"sys",  "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"idle", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"softirq", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"intr", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"steal", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"user", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]}
   ]
}
{
    "cluster": "testcluster",
    "instance": "i-03f8f8c9e7febab21",
    "instype": "r4.8xlarge",
    "AZ": "us-east-1e",
    "env": "test", 
    "cpus":  "32",
    "memory": "256G",
    "kernel": "4.4.0-96-generic",
    "info": "cpu usage stats",
    "unit": "percent",
    "cpustats": {
          "user":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "sys":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "idle":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "softirq":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "intr":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "steal":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]
    }
}

如何在php中构建?

对不起,这个问题解释得很糟糕!!如果我理解的话,你想要一个类似上面的JSON(你的帖子)

PHP有一个特殊的函数来管理JSON对象:JSON_编码和JSON_解码


使用json_编码 它用于将对象转换为JSON字符串

<?php
/** You can also use Arrays (Ex. $myArray = array(...)) */
$myObject = new StdClass();

$myObject->newKey = "newValue";
$myObject->otherKey = "otherValue";

$myObject->nestedArrays = array(
    'array1' => array(
        'key1' => 'value1',
        'key2' => 'value2', 
        /** ... OTHER KEY-VALUE ... */
        'keyN' => 'vakyeN'
    ),

    'array2' => array(
        'key1' => 'value1',
        'key2' => 'value2', 
        /** ... OTHER KEY-VALUE ... */
        'keyN' => 'vakyeN'
    )
);

/** ... OTHER CODE ... */

$myJSONString = json_encode($myObject);

print_r($myJSONString);

/** OUTPUT
  * {
  *     "newKey":"newValue",
  *     "otherKey":"otherValue",
  *     "nestedArrays":{
  *         "array1":{
  *             "key1":"value1",
  *             "key2":"value2",
  *             "keyN":"vakyeN"
  *         },
  *         "array2":{
  *             "key1":"value1",
  *             "key2":"value2",
  *             "keyN":"vakyeN"
  *         }
  *     }
  * }
  */

?>
<?php

$myObject = json_decode($myJSONString);

?>
代码: 输出:
首先显示您的代码。我们不是代码生成器。这是你最好的新朋友第1步:做一系列研究。第二步:试试看。第三步:如果你在某些特定的问题上遇到了困难,而你却无法找到在线解决方案,请回来告诉我们你尝试了什么,遇到了什么困难,预期的输出和实际的输出,我们可以从中帮助你。
$obj = new stdClass();
$obj->cluster="testcluster";
$obj->instance="i-03f8f8c9e7febab21";
$obj->instype="r4.8xlarge";
$obj->AZ="us-east-1e",
$obj->env="test",
$obj->cpus="32";
$obj->memory="25G";
$obj->kernelversion="4.4.0-96-generic";
$obj->info="cpu usage stats";
$obj->unit="percent";
$obj->cpustats = new stdClass();
$obj->cpustats->usr = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->sys = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->idle = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->softirq = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->intr = array (1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->steal = array (1874,1857,1884,1869,1909,1912,1901,1880,1883); 

var_dump(json_encode($obj));
string(555) "{
"cluster":"testcluster",
"instance":"i-03f8f8c9e7febab21",
"instype":"r4.8xlarge",
"AZ":"us-east1e",
"env":"test",
"cpus":"32",
"memory":"25G",
"kernelversion":"4.4.0-96-generic",
"info":"cpu usage stats",
"unit":"percent",
"cpustats":{
"usr":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"sys":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"idle":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"softirq":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"intr":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"steal":[1874,1857,1884,1869,1909,1912,1901,1880,1883]
}"