我想合并具有相同名称的数组,并在单个数组中显示所有相同的名称。
我在下面显示数组
Array
(
[0] => Array
(
[location_name] => NTPL Vault
)
[1] => Array
(
[location_name] => NTPL Safe Room
)
[2] => Array
(
[location_name] => Safe NTPL
)
)
$array = call_user_func_array('array_merge', $myArray);
我期望输出如下...
[
{
"location_name": "NTPL"
},
{
"location_name": "NJKL"
},
{
"location_name": "KLDF"
}
]
答案 0 :(得分:1)
所需的输出为JSON格式,PHP中有两个常用功能:
要将数组从PHP形式更改为JSON,可以使用json_encode
,反之亦然:
$array = array
(
'0' => array
(
'location_name' => 'NTPL Vault',
),
'1' => array
(
'location_name' => 'NTPL Safe Room',
),
'2' => array
(
'location_name' => 'Safe NTPL',
),
);
$output = json_encode($array);
var_dump($output);
string(97) "[{"location_name":"NTPL Vault"},{"location_name":"NTPL Safe Room"},{"location_name":"Safe NTPL"}]"
如果您想更改location_name
的值,则可以简单地使用其他功能。