我不确定用于我的标题的适当术语是什么,但只是想知道在下面的解决方案需要在关联数组中合并/组合两个值.
例如,我有这个数组:
Array
(
[1] => Array
(
[agent_name_1] => Agent 1
[agent_phone_1] => 0123456
[agent_company_1] => My Company
[agent_email_1] => agent@yahoo.com
[agent_address_1] => United States
)
)
在这里,我想将company_1与address_1结合使用.所以输出应该是这样的:
Array
(
[1] => Array
(
[agent_name_1] => Agent 1
[agent_phone_1] => 0123456
[agent_email_1] => agent@yahoo.com
[agent_address_1] => My Company, United States
)
)
解决方法:
您需要循环遍历数组并修改原始数组的相应键.
foreach ($array as $key => $agent) {
$array[$key]['agent_address_1'] = $agent['agent_company_1'] . ', ' . $agent['agent_address_1'];
unset($array[$key]['agent_company_1']);
}
阅读有关foreach控制结构及其如何循环遍历数组的更多信息.