【问题标题】:In PHP, how do you output JSON iterating first on a child array, then on parent?在 PHP 中,如何输出 JSON 首先在子数组上迭代,然后在父数组上迭代?
【发布时间】:2016-02-25 00:37:39
【问题描述】:

我已经尝试了几个星期来让它工作,并且已经放弃了。我需要在 JSON 中显示以下数组。

如您所见,[coloroptions] 本身就是一个 JSON 数组,使其成为一个多维数组。诀窍是(我认为)迭代数组,首先在子 [coloroptions] 上,然后在父级上。不过,我可能离基地很远。有人做过这样的事吗?

提前谢谢你!

样本数据来自 print_r()

    Array
    (
        [0] => Array
            (
                [id] => 1
                [automobile] => 'Mustang'
                [coloroptions] => [{"color":"red"},{"color":"blue"},{"color":"green"}]
            )

        [1] => Array
            (
                [id] => 2
                [automobile] => 'Camero'
                [coloroptions] =>  [{"color":"red"},{"color":"orange"}]
            )
    )

JSON 输出

    [
        {
            "id": 1,
            "automobile": "Mustang",
            "color": "red"
        },
        {
            "id": 1,
            "automobile": "Mustang",
            "color": "blue"
        },
        {
            "id": 1,
            "automobile": "Mustang",
            "color": "green"
        },
        {
            "id": 2,
            "automobile": "Camero",
            "color": "red"
        },
        {
            "id": 2,
            "automobile": "Camero",
            "color": "orange"
        },
    ]

【问题讨论】:

  • “JSON 输出”是所需的输出吗?
  • 是的,示例中的 JSON 是我试图通过提供的数据实现的。该项目更深入,但对如何处理它有基本的了解,会让我朝着正确的方向前进。非常感谢!

标签: php arrays json multidimensional-array


【解决方案1】:

我想你很接近了!我的方法是在颜色上使用json_decode,然后像普通的 PHP 数组一样遍历它们。

// First, start by looping through each 'parent':
foreach($array as $key => $value)
{
    // Decode the json color array into a normal php array
    $colorarray = json_decode($value['coloroptions'], true);

    /* Loop through each of the colours.
       (Note that they'll be a few layers deep. 
       Do a print_r($colorarray) to see it's structure)  */
    foreach($colorarray as $color)
    {
       // And build your output array:
        $output[] = array(
            "id" => $value['id'], 
            "automobile" => $value['automobile'],
            "color" => $color['color']
        );
    }
}

要检查最终的 PHP 数组,您可以print_r($output)。要将$output 转换为json 数组,请使用json_encode

json_encode($output);

【讨论】:

    【解决方案2】:

    好吧,您需要json_decode coloroptions 并将其中包含颜色的组装数组输出到结果数组。

    $a = array(
        array(
            'id'           => 1,
            'automobile'   => 'Mustang',
            'coloroptions' => '[{"color":"red"},{"color":"blue"},{"color":"green"}]'
            ),
        array(
            'id'           => 2,
            'automobile'   => 'Camero',
            'coloroptions' => '[{"color":"red"},{"color":"orange"}]'
            ),
        );
    
    $result = array();
    foreach($a as $key => $value) {
        $coloroptions = json_decode($value['coloroptions']);
        foreach($coloroptions as $color){
            $result[] = array(
                'id'         => $value['id'],
                'automobile' => $value['automobile'],
                'color'      => $color->color,
                );
        }
    }
    print_r(json_encode($result));
    

    查看示例here

    【讨论】:

    • 太棒了!您在几分钟内解决了困扰我数周的问题。这非常有效。感谢您提供沙盒示例。我缺少的最大部分是孩子的 json_decode。
    猜你喜欢
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2019-12-18
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多