简体   繁体   English

为什么 array_merge_recursive() 不返回所需的输出 PHP

[英]Why is array_merge_recursive() not returning required output PHP

I have the following string that I am trying to combine the inner array stop recursively and print the result as json data but seems like the array_merge_recursive() function is not producing the desired output.我有以下字符串,我试图递归地组合内部数组停止并将结果打印为 json 数据,但似乎array_merge_recursive()函数没有产生所需的输出。

The result I am getting whenever I run the code is the same.每次运行代码时得到的结果都是一样的。 Am I missing something or is there an alternative function to achieve combining the inner array?我是否遗漏了什么或者是否有替代函数来实现组合内部数组?

$routes = '[
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "1",
                "name": "City Centre (Pangani)"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "2",
                "name": "Muthaiga"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "3",
                "name": "Utalii"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "4",
                "name": "KCA"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "5",
                "name": "Drive Inn"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "6",
                "name": "Allsops"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "7",
                "name": "GSU"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "8",
                "name": "Roasters"
            }
        ]
    },
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "9",
                "name": "Ngumba"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "1",
                "name": "City Centre (Pangani)"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "2",
                "name": "Muthaiga"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "3",
                "name": "Utalii"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "4",
                "name": "KCA"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "5",
                "name": "Drive Inn"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "6",
                "name": "Allsops"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "7",
                "name": "GSU"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "8",
                "name": "Roasters"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "9",
                "name": "Ngumba"
            }
        ]
    }
]';

This is the desired output这是所需的输出

[
    {
        "id": "1",
        "name": "CBD - Ngumba via Thika Road",
        "stop": [
            {
                "id": "1",
                "name": "City Centre (Pangani)"
            },
            {
                "id": "2",
                "name": "Muthaiga"
            },
            {
                "id": "3",
                "name": "Utalii"
            },
            {
                "id": "4",
                "name": "KCA"
            },
            {
                "id": "5",
                "name": "Drive Inn"
            },
            {
                "id": "6",
                "name": "Allsops"
            },
            {
                "id": "7",
                "name": "GSU"
            },
            {
                "id": "8",
                "name": "Roasters"
            },
            {
                "id": "9",
                "name": "Ngumba"
            }
        ]
    },
    {
        "id": "2",
        "name": "Ngumba - City Centre via Thika Road",
        "stop": [
            {
                "id": "1",
                "name": "City Centre (Pangani)"
            },
            {
                "id": "2",
                "name": "Muthaiga"
            },
            {
                "id": "3",
                "name": "Utalii"
            },
            {
                "id": "4",
                "name": "KCA"
            },
            {
                "id": "5",
                "name": "Drive Inn"
            },
            {
                "id": "6",
                "name": "Allsops"
            },
            {
                "id": "7",
                "name": "GSU"
            },
            {
                "id": "8",
                "name": "Roasters"
            },
            {
                "id": "9",
                "name": "Ngumba"
            }
        ]
    }
]

Here is what I have tried but still returns the data as is in the original string.这是我尝试过的,但仍然返回原始字符串中的数据。

$routes = json_decode($routes);
echo json_encode(array_merge_recursive($routes));

It's not working because that's not how the function works.它不起作用,因为这不是该功能的工作方式。 You need to write a custom function to achieve desired result.您需要编写一个自定义函数来实现所需的结果。

function merge_result($data) 
{
   $result = [];
   $index = 0;
   $keys = [];
   foreach($data as $row) 
   {
       if(array_key_exists($row['id'], $keys)){
           $_index = $keys[$row['id']];
           $result[$_index]['stop'] = array_merge($result[$_index]['stop'], $row['stop']);
       }else {
           $keys[$row['id']] = $index; 
           $result[$index] = $row; 
          $index++;
       }

   }
  return $result;
}

$routes = merge_result(json_decode($data, true));

PS: Am unable to test because am on mobile phone. PS:我无法测试,因为我在手机上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM