[英]Remove parent array from multidimensional array in php
using php how to get array result into below way, 使用php如何将数组结果导入到下面的方式,
Array
(
[3] => Array
(
[15] => 15
[16] => 16
[17] => 17
[18] => 18
[19] => 19
)
)
how to convert above array into below format, 如何将上面的数组转换成以下格式,
Array
(
[0] => 15
[1] => 16
[2] => 17
[3] => 18
[4] => 19
)
array_values()
is your friend; array_values()
是你的朋友;
Presuming your array exists in a variable called $array
; 假设你的数组存在于一个名为$array
的变量中;
$newArray = array_values($array[3]);
try this, if you have more than one sub-array, it will work. 试试这个,如果你有多个子阵列,它会起作用。
$arr = array(3 =>
array
(
15 => 15,
16 => 16,
17 => 17,
18 => 18,
19 => 19
)
);
$new = array();
foreach ($arr as $v){
$new = array_merge($new , array_values($v)) ;
}
echo "<pre>"; print_r($new);
Have not tested it but should work as per your requirement. 没有测试过,但应该按照您的要求工作。
<?php
$parent array = array(); // The array which you want to change
$result_array = array(); // The array that will hold the results
foreach( $parent_array as $child_array )
{
if( is_array( $child_array ) )
{
foreach( $child_array as $element )
{
$result_array[] = $element
}
}
}
echo '<pre>';
print_r($result_array);
echo '</pre>';
?>
you should use RecursiveArrayIterator to remove parent array 您应该使用RecursiveArrayIterator删除父数组
$arr = new RecursiveIteratorIterator(new RecursiveArrayIterator($multidimensional_array));
$new_arr = iterator_to_array($arr, false);
it's pretty simple, as you can just assign the array holding variable to the values of one index … 它非常简单,因为您可以将数组保持变量分配给一个索引的值...
<?php
/* build list */
for($i=15;$i<=19;$i++)
$b[$i] = $i;
$a[3] = $b;
var_export($a);
/* make array smaller again */
$a = $a[3];
var_export($a);
/* reindexing, just values */
$a = array_values( $a );
var_export($a);
?>
the reindexing part is done by a build-in function, also have a look on php.net for the linked functions for arrays, you can do massive stuff simple with them. 重建索引部分是通过内置函数完成的,也可以在php.net上查看数组的链接函数,你可以用它们做大量的简单操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.