Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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_Json - Fatal编程技术网

Php 为json输出构建多维数组

Php 为json输出构建多维数组,php,json,Php,Json,希望以以下格式输出json: [{"Montgomery":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Suburban":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Shady Grove

希望以以下格式输出json:

 [{"Montgomery":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Suburban":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Shady Grove Adventist":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Holy Cross":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Washington Adventist":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}]}]
我的代码:

 $xyz[] = array("Montgomery"=> array("Red" => "12:00", "Yellow" => "14:00"));
 $xyz[] = array("Suburban"=> array("Yellow" => "16:00"));

 echo '[' . json_encode($xyz) . ']';
我的结果:

[[{"Montgomery":{"Red":"12:00","Yellow":"14:00"}},{"Suburban":{"Yellow":"16:00"}}]] 

这将为您提供以下结构:

$container = array();
$container['Montgomery'] = array(array('red' => '12:34:56', 'yellow' => '11:44:46'));
$container['Suburban'] = array(array('red' => '12:34:56', 'yellow' => '11:44:46'));
echo json_encode(array($container));

您可以使用对象,类似于这样的东西您需要稍微清理一下:

class Church {
  public $red = "";
  public $yellow = "";
  public $orange = "";
  public $green = "";
  public $purple = "";
}
class XYZ {
  public $Montgomery = new Church();
  public $Shad_Grove_Adventist = new Church();
  public $Holy_Cross = new Church();
  public $Washington_Adventist = new Church();
}

$xyz = new XYZ();
$xyz->Montgomery->red = "12:00";
...
然后输出JSON:

echo '[' . json_encode($xyz) . ']';

它不会与您想要的JSON输出完美匹配,但它会提供更好的可读性和更大的灵活性。

您想要的输出有一个数组[]作为其顶级项。JSON中的[]对应于PHP中的数字数组。JSON数组用于包含有序的项集合

顶级数组包含一个项目,即JSON对象{}。PHP对象stdClass或关联数组将转换为JSON格式。JSON对象用于创建键值对集合

要生成所需的输出,请使用PHP构建数据,如下所示:

// Top-level numeric array.
$container = array();

// Only item in top-level array.
$item = array();

// The value of each of these items will be a numeric array.
$item['Montgomery'] = array();

// Create a new associative array, and push that onto the list array.
$subItem = array("red" => "12:34:56",
        "yellow" => "11:44:46",
        "orange" => "10:54:36",
        "green" => "9:24:26",
        "purple" => "8:14:16");
$item['Montgomery'][] = $subItem;
$container[] = $item; 

// ...Add more items...

print json_encode($container);
或者,如果您想一次全部构建:

$container = array(
    array(
        "Montgomery" => array(
            array("red" => "12:34:56",
                "yellow" => "11:44:46",
                "orange" => "10:54:36",
                "green" => "9:24:26",
                "purple" => "8:14:16"
            )
        )
        // ...More items...
    )
);

print json_encode($container);

请注意,有些位置在数组中具有数组。这是为了构建关联数组并将其添加为数字数组的唯一成员。这相当于在[]内有一个{}。

这是一个带空格的有效变量吗?公众$Shad_Grove基督复临安息日会;抱歉,它需要下划线。太好了,这正是我想要的@wwwslinger:我想把它们都保存在一个数组中,而不是创建一个类,但是非常好的选择,谢谢你们。