从多个多维数组中获取所有值

时间:2021-01-23 21:38:27

I want to nest this multiple multi-dimensional array below, without using any loops.

我想在下面嵌套这个多维多维数组,而不使用任何循环。

I tried some very good examples over here that works for associative or multi dimensional arrays but couldn't put them together in a good use to solve my problem:

我在这里尝试了一些非常好的例子,它们适用于关联或多维数组但不能很好地将它们放在一起以解决我的问题:

Array
(
    [0] => Array
        (
            [level2_1] => aa
            [level2_2] => bb
            [level2_3] => cc
            [level2_4] => Array
                (
                    [level3_1] => DDD
                    [level3_2] => EEE
                )

        )

    [1] => Array
        (
            [level2_1] => ff
            [level2_2] => gg
            [level2_3] => hh
            [level2_4] => Array
                (
                    [level3_1] => III
                    [level3_2] => JJJ
                )

        )

)

Expected output:

0 -> level2_1: aa, level2_2: bb, level2_3: cc, level3_1: DDD, level3_2: EEE
1 -> level2_1: ff, level2_2: gg, level2_3: hh, level3_1: III, level3_2: JJJ

Or At Least:

或者至少:

0: aa, bb, cc, DDD, EEE
1: ff, gg, hh, III, JJJ

I tried many non-loop answers regarding implode, multi-dimension, etc. I think they need some modification if one of the element (level2_4) is array instead of string, flatten its elements.

我尝试了许多关于内爆,多维等的非循环答案。我认为如果其中一个元素(level2_4)是数组而不是字符串,则需要进行一些修改,使其元素变平。

Here are some good samples of code I found working but needs to be modified to fit my case:

以下是我发现的一些很好的代码示例,但需要进行修改以适合我的情况:

# Basic but stops at a sub-array:

#Basic但在子数组处停止:

echo implode(',', call_user_func_array('array_merge', $data));

echo implode(',',call_user_func_array('array_merge',$ data));

# PHP implode associative array:

#PHP implode associative array:

implode(', ', array_map(function ($k, $v) { return "$k: $v"; }, array_keys($data), array_values($data)));

implode(',',array_map(function($ k,$ v){return“$ k:$ v”;},array_keys($ data),array_values($ data)));

# PHP implode multi dimensional array:

#PHP implode多维数组:

$result[0][code] = "123";

$ result [0] [code] =“123”;

$result[0][desc] = "Desc123";

$ result [0] [desc] =“Desc123”;

$result[1][code] = "567";

$ result [1] [code] =“567”;

$result[1][desc] = "Desc567";

$ result [1] [desc] =“Desc567”;

echo implode(', ', array_map(function ($k, $v) { return "$k: $v"; }, array_keys(array_column($result, 'desc', 'code')), array_values(array_column($result, 'desc', 'code'))));

echo implode(',',array_map(function($ k,$ v){return“$ k:$ v”;},array_keys(array_column($ result,'desc','code')),array_values(array_column( $ result,'desc','code'))));

3 个解决方案

#1


3  

With combination of array_keys + array_walk_recursive + array_map functions:

结合使用array_keys + array_walk_recursive + array_map函数:

// $arr is your initial array
$result = array_map(function($k) use($arr){
    $s = "$k ->";   // key prefix
    array_walk_recursive($arr[$k], function($v, $k) use(&$s){
        $s .= " $k: $v,";
    });
    return trim($s, ',');
}, array_keys($arr));

print_r($result);

The output:

Array
(
    [0] => 0 -> level2_1: aa, level2_2: bb, level2_3: cc, level3_1: DDD, level3_2: EEE
    [1] => 1 -> level2_1: ff, level2_2: gg, level2_3: hh, level3_1: III, level3_2: JJJ
)

#2


0  

May be, you can try a different way like this:

可能是,您可以尝试这样的不同方式:

<?php
function Recursive($data){
$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$result = array();
foreach ($ritit as $leafValue) {
$keys = array();
foreach (range(0, $ritit->getDepth()) as $depth) {
$keys[] = $ritit->getSubIterator($depth)->key();
}
$result[ join('.', $keys) ] = $leafValue;
}
return $result;
}

$data =
[
        [
            'level2_1' => 'aa',
            'level2_2' => 'bb',
            'level2_3' => 'cc',
            'level2_4' =>
                [
                    'level3_1' => 'DDD',
                    'level3_2' => 'EEE'
                ]
         ],
         [
            'level2_1' => 'ff',
            'level2_2' => 'gg',
            'level2_3' => 'hh',
            'level2_4' =>
                [
                    'level3_1' => 'III',
                    'level3_2' => 'JJJ'
                ]
        ]
];  

$recursive = Recursive($data);
echo "<pre>".print_r($recursive,1)."<pre>";
?>

Output:

Array
(
    [0.level2_1] => aa
    [0.level2_2] => bb
    [0.level2_3] => cc
    [0.level2_4.level3_1] => DDD
    [0.level2_4.level3_2] => EEE
    [1.level2_1] => ff
    [1.level2_2] => gg
    [1.level2_3] => hh
    [1.level2_4.level3_1] => III
    [1.level2_4.level3_2] => JJJ
)

#3


0  

Pretty much there but for the trailing commas.

几乎只有那些尾随的逗号。

<?php

$data =
[
        [
            'level2_1' => 'aa',
            'level2_2' => 'bb',
            'level2_3' => 'cc',
            'level2_4' =>
                [
                    'level3_1' => 'DDD',
                    'level3_2' => 'EEE'
                ]
         ],
         [
            'level2_1' => 'ff',
            'level2_2' => 'gg',
            'level2_3' => 'hh',
            'level2_4' =>
                [
                    'level3_1' => 'III',
                    'level3_2' => 'JJJ'
                ]
        ]
];  

array_walk($data, function($v, $k) {
    print "$k ->";
    array_walk_recursive($v, function($v, $k) {
        printf(' %s: %s,', $k, $v);
    });
    print "\n";
});

Output:

0 -> level2_1: aa, level2_2: bb, level2_3: cc, level3_1: DDD, level3_2: EEE,
1 -> level2_1: ff, level2_2: gg, level2_3: hh, level3_1: III, level3_2: JJJ,

And to fix the trailing commas swap the printf line to:

并修复尾随逗号将printf行交换为:

print rtrim(sprintf(' %s: %s,', $k, $v),',');

#1


3  

With combination of array_keys + array_walk_recursive + array_map functions:

结合使用array_keys + array_walk_recursive + array_map函数:

// $arr is your initial array
$result = array_map(function($k) use($arr){
    $s = "$k ->";   // key prefix
    array_walk_recursive($arr[$k], function($v, $k) use(&$s){
        $s .= " $k: $v,";
    });
    return trim($s, ',');
}, array_keys($arr));

print_r($result);

The output:

Array
(
    [0] => 0 -> level2_1: aa, level2_2: bb, level2_3: cc, level3_1: DDD, level3_2: EEE
    [1] => 1 -> level2_1: ff, level2_2: gg, level2_3: hh, level3_1: III, level3_2: JJJ
)

#2


0  

May be, you can try a different way like this:

可能是,您可以尝试这样的不同方式:

<?php
function Recursive($data){
$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$result = array();
foreach ($ritit as $leafValue) {
$keys = array();
foreach (range(0, $ritit->getDepth()) as $depth) {
$keys[] = $ritit->getSubIterator($depth)->key();
}
$result[ join('.', $keys) ] = $leafValue;
}
return $result;
}

$data =
[
        [
            'level2_1' => 'aa',
            'level2_2' => 'bb',
            'level2_3' => 'cc',
            'level2_4' =>
                [
                    'level3_1' => 'DDD',
                    'level3_2' => 'EEE'
                ]
         ],
         [
            'level2_1' => 'ff',
            'level2_2' => 'gg',
            'level2_3' => 'hh',
            'level2_4' =>
                [
                    'level3_1' => 'III',
                    'level3_2' => 'JJJ'
                ]
        ]
];  

$recursive = Recursive($data);
echo "<pre>".print_r($recursive,1)."<pre>";
?>

Output:

Array
(
    [0.level2_1] => aa
    [0.level2_2] => bb
    [0.level2_3] => cc
    [0.level2_4.level3_1] => DDD
    [0.level2_4.level3_2] => EEE
    [1.level2_1] => ff
    [1.level2_2] => gg
    [1.level2_3] => hh
    [1.level2_4.level3_1] => III
    [1.level2_4.level3_2] => JJJ
)

#3


0  

Pretty much there but for the trailing commas.

几乎只有那些尾随的逗号。

<?php

$data =
[
        [
            'level2_1' => 'aa',
            'level2_2' => 'bb',
            'level2_3' => 'cc',
            'level2_4' =>
                [
                    'level3_1' => 'DDD',
                    'level3_2' => 'EEE'
                ]
         ],
         [
            'level2_1' => 'ff',
            'level2_2' => 'gg',
            'level2_3' => 'hh',
            'level2_4' =>
                [
                    'level3_1' => 'III',
                    'level3_2' => 'JJJ'
                ]
        ]
];  

array_walk($data, function($v, $k) {
    print "$k ->";
    array_walk_recursive($v, function($v, $k) {
        printf(' %s: %s,', $k, $v);
    });
    print "\n";
});

Output:

0 -> level2_1: aa, level2_2: bb, level2_3: cc, level3_1: DDD, level3_2: EEE,
1 -> level2_1: ff, level2_2: gg, level2_3: hh, level3_1: III, level3_2: JJJ,

And to fix the trailing commas swap the printf line to:

并修复尾随逗号将printf行交换为:

print rtrim(sprintf(' %s: %s,', $k, $v),',');