I have the following multi-dimensional array
我有以下多维数组
$sample = array(
'1232' => 'Nokia 72',
'234' => array(
'534' => 'Samsung 58',
'345' => 'Samsung 64'
),
'3445' => 'Micromax 1542c',
'542' => array(
'4645' => 'LG 58',
'5765' => 'LG 64'
)
);
Now, I want to collect only values of every values of each section.
现在,我只想收集每个部分每个值的值。
My output should be like below
我的输出应该如下所示
Array
(
[0] => Nokia 72
[1] => Samsung 58
[2] => Samsung 64
[3] => Micromax 1542c
[4] => LG 58
[5] => LG 64
)
I don't want to do it with foreach function.
我不想用foreach函数。
4 个解决方案
#1
2
You can do it by using
你可以用
array_walk_recursive($sample, create_function('$val, $key, $obj', 'array_push($obj, $val);'), &output);
You can also get some more reference from below link
你也可以从下面的链接得到更多的参考
How-to-filter-only-values-from-complex-multi-dimensional-array
How-to-filter-only-values-from-complex-multi-dimensional-array
#2
2
$sample = array(
'1232' => 'Nokia 72',
'234' => array(
'534' => 'Samsung 58',
'345' => 'Samsung 64'
),
'3445' => 'Micromax 1542c',
'542' => array(
'4645' => 'LG 58',
'5765' => 'LG 64'
)
);
array_walk_recursive($sample, function($a) use (&$return) { $return[] = $a; });
var_dump($return);
Output:
输出:
array(6) { [0]=> string(8) "Nokia 72" [1]=> string(10) "Samsung 58" [2]=> string(10) "Samsung 64" [3]=> string(14) "Micromax 1542c" [4]=> string(5) "LG 58" [5]=> string(5) "LG 64" }
Here is a PHP Sandbox with the demo: http://sandbox.onlinephpfunctions.com/
下面是一个PHP沙箱,带有演示:http://sandbox.onlinephpfunctions.com/
This solution uses array_walk_recursive() and PHP anonymous functions.
这个解决方案使用array_walk_recursive()和PHP匿名函数。
http://php.net/array_walk_recursive
http://php.net/array_walk_recursive
http://php.net/manual/en/functions.anonymous.php
http://php.net/manual/en/functions.anonymous.php
#3
1
RecursiveIteratorIterator
it returns a flattened array when you used with iterator_to_array
function.
当您使用iterator_to_array函数时,它返回一个扁平数组。
演示
$sample = array(
'1232' => 'Nokia 72',
'234' => array(
'534' => 'Samsung 58',
'345' => 'Samsung 64'
),
'3445' => 'Micromax 1542c',
'542' => array(
'4645' => 'LG 58',
'5765' => 'LG 64'
)
);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($sample));
$l = iterator_to_array($it, false);
echo '<pre>';print_r($l);echo '</pre>';
#4
0
If you want to implement it on your own with a recursive function:
如果你想用递归函数自己实现它:
$sample = array(
'1232' => 'Nokia 72',
'234' => array(
'534' => 'Samsung 58',
'345' => 'Samsung 64'
),
'3445' => 'Micromax 1542c',
'542' => array(
'4645' => 'LG 58',
'5765' => 'LG 64'
)
);
// This recursive function changes the original values!
function walk(&$multi_dim_array, &$flat_array)
{
while (sizeof($multi_dim_array) > 0)
{
// Take the first element
$curr_value = array_shift($multi_dim_array);
// If this item is an array go one step deeper
if (is_array($curr_value))
walk($curr_value, $flat_array);
else
// The current value is not an array
// append it to the flattened array
$flat_array[] = $curr_value;
}
}
$values = [];
walk($sample, $values);
print_r($values);
Outputs
输出
Array ( [0] => Nokia 72 [1] => Samsung 58 [2] => Samsung 64 [3] => Micromax 1542c [4] => LG 58 [5] => LG 64 )
#1
2
You can do it by using
你可以用
array_walk_recursive($sample, create_function('$val, $key, $obj', 'array_push($obj, $val);'), &output);
You can also get some more reference from below link
你也可以从下面的链接得到更多的参考
How-to-filter-only-values-from-complex-multi-dimensional-array
How-to-filter-only-values-from-complex-multi-dimensional-array
#2
2
$sample = array(
'1232' => 'Nokia 72',
'234' => array(
'534' => 'Samsung 58',
'345' => 'Samsung 64'
),
'3445' => 'Micromax 1542c',
'542' => array(
'4645' => 'LG 58',
'5765' => 'LG 64'
)
);
array_walk_recursive($sample, function($a) use (&$return) { $return[] = $a; });
var_dump($return);
Output:
输出:
array(6) { [0]=> string(8) "Nokia 72" [1]=> string(10) "Samsung 58" [2]=> string(10) "Samsung 64" [3]=> string(14) "Micromax 1542c" [4]=> string(5) "LG 58" [5]=> string(5) "LG 64" }
Here is a PHP Sandbox with the demo: http://sandbox.onlinephpfunctions.com/
下面是一个PHP沙箱,带有演示:http://sandbox.onlinephpfunctions.com/
This solution uses array_walk_recursive() and PHP anonymous functions.
这个解决方案使用array_walk_recursive()和PHP匿名函数。
http://php.net/array_walk_recursive
http://php.net/array_walk_recursive
http://php.net/manual/en/functions.anonymous.php
http://php.net/manual/en/functions.anonymous.php
#3
1
RecursiveIteratorIterator
it returns a flattened array when you used with iterator_to_array
function.
当您使用iterator_to_array函数时,它返回一个扁平数组。
演示
$sample = array(
'1232' => 'Nokia 72',
'234' => array(
'534' => 'Samsung 58',
'345' => 'Samsung 64'
),
'3445' => 'Micromax 1542c',
'542' => array(
'4645' => 'LG 58',
'5765' => 'LG 64'
)
);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($sample));
$l = iterator_to_array($it, false);
echo '<pre>';print_r($l);echo '</pre>';
#4
0
If you want to implement it on your own with a recursive function:
如果你想用递归函数自己实现它:
$sample = array(
'1232' => 'Nokia 72',
'234' => array(
'534' => 'Samsung 58',
'345' => 'Samsung 64'
),
'3445' => 'Micromax 1542c',
'542' => array(
'4645' => 'LG 58',
'5765' => 'LG 64'
)
);
// This recursive function changes the original values!
function walk(&$multi_dim_array, &$flat_array)
{
while (sizeof($multi_dim_array) > 0)
{
// Take the first element
$curr_value = array_shift($multi_dim_array);
// If this item is an array go one step deeper
if (is_array($curr_value))
walk($curr_value, $flat_array);
else
// The current value is not an array
// append it to the flattened array
$flat_array[] = $curr_value;
}
}
$values = [];
walk($sample, $values);
print_r($values);
Outputs
输出
Array ( [0] => Nokia 72 [1] => Samsung 58 [2] => Samsung 64 [3] => Micromax 1542c [4] => LG 58 [5] => LG 64 )