[英]Flatten a multi-dimensional array, whilst removing an element from each
Is it possible to flatten a multi-dimensional array, whilst also removing an element from each sub-array?是否可以展平多维数组,同时从每个子数组中删除一个元素?
Currently, I am storing two elements per sub-array, like so:目前,我为每个子数组存储两个元素,如下所示:
Array (
[billing_first_name] => Array (
[0] => Test
[1] => 1
)
[billing_last_name] => Array (
[0] => Test
[1] => 1
)
)
But I need to remove the second sub-element, flattening the array to:但我需要删除第二个子元素,将数组展平为:
Array (
[billing_first_name] => Test
[billing_last_name] => Test
)
I had thought that this could be possible through a foreach, but after removing the 2nd element from the sub-array, I'm unsure what route would be most efficient to flatten the array.我原以为这可以通过 foreach 实现,但是从子数组中删除第二个元素后,我不确定哪种路由最有效地展平数组。
foreach( $customer_data_new as $key => $value ) {
unset($customer_data_new[$key][1]);
}
If anyone could explain the best option, I would be graatful.如果有人可以解释最佳选择,我将不胜感激。
Can you try the below code你能试试下面的代码吗
$customer_data_new = array(
'billing_first_name' => array(
'0' =>'Test',
'1' => 1
),
'billing_last_name' => array(
'0' =>'Test',
'1' => 1
)
);
$newData = array();
foreach( $customer_data_new as $key => $value ) {
$newData[$key] = $value[0];
}
print_r($newData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.