PHP将一个多维数组的子级合并到另一个内联中

时间:2018-08-09 06:30:49

标签: php arrays

我已经爬了好几个小时,试图找到解决办法,但我做不到。

情况:假设您要设置一个多维数组,并想使用一个返回多维数组的函数来设置其某些值。

挑战在于,因为您要使用该函数返回多个数组,所以您基本上只需要获取返回数组的子数组并将其插入父级。你做什么?

下面是一些示例代码:

function some_function(){
    $items = array();

    $items[] = array(
        'id'                => 'thing2',
        'type'              => 'sample',
    );

    $items[] = array(
        'id'                => 'thing3',
        'type'              => 'sample',
    );

    return $items;
}

$ztest = array(
    array(
        'id'                => 'thing1',
        'type'              => 'sample',
    ),
    some_function(),
    array(
        'id'                => 'thing4',
        'type'              => 'sample',
    ),
    array(
        'id'                => 'thing5',
        'type'              => 'sample',
    ),
);


print_r($ztest);

我想要的输出将是:

Array
(
[0] => Array
    (
        [id] => thing1
        [type] => sample
    )

[1] => Array
    (
        [id] => thing2
        [type] => sample
    )

[2] => Array
    (
        [id] => thing3
        [type] => sample
    )

[3] => Array
    (
        [id] => thing4
        [type] => sample
    )

[4] => Array
    (
        [id] => thing5
        [type] => sample
    )
)

但是无论我尝试什么,我最终都只能得到:

Array
(
[0] => Array
    (
        [id] => thing1
        [type] => sample
    )

[1] => Array
    (
        [0] => Array
            (
                [id] => thing2
                [type] => sample
            )

        [1] => Array
            (
                [id] => thing3
                [type] => sample
            )

    )

[2] => Array
    (
        [id] => thing4
        [type] => sample
    )

[3] => Array
    (
        [id] => thing5
        [type] => sample
    )
)

我已经尝试过array_map,array_filter,array_values,array_merge ...似乎没有任何作用。

这看起来很简单,我觉得应该可以做,但我不知道。

注意:我知道我可以在事后对这两个数组进行array_merge,也可以在事后再插入它,但是为了代码整洁,我更喜欢进行内联。

非常感谢您!

-扎克

2 个答案:

答案 0 :(得分:0)

尝试此功能: array_merge_recursive 这是示例代码:

   <?php 
    function some_function(){
    $items = array();

    $items[] = array(
        'id'                => 'thing2',
        'type'              => 'sample',
    );

    $items[] = array(
        'id'                => 'thing3',
        'type'              => 'sample',
    );

    return $items;
}


$ztest = array(
    array(
        'id'                => 'thing1',
        'type'              => 'sample',
    ),

    array(
        'id'                => 'thing4',
        'type'              => 'sample',
    ),
    array(
        'id'                => 'thing5',
        'type'              => 'sample',
    ),
);
$result = array_merge_recursive($ztest, some_function());
echo "<pre>";

print_r($result);
    ?>

答案 1 :(得分:0)

function some_function(){
    $items = array();

    $items[] = array(
        'id'                => 'thing2',
        'type'              => 'sample',
    );

    $items[] = array(
        'id'                => 'thing3',
        'type'              => 'sample',
    );

    return $items;
}

$ztest = array_merge(some_function(), array(
    array(
        'id'                => 'thing1',
        'type'              => 'sample',
    ),
    array(
        'id'                => 'thing4',
        'type'              => 'sample',
    ),
    array(
        'id'                => 'thing5',
        'type'              => 'sample',
    ),
));

然后,如果您需要按id字段对数组项进行排序,请使用usort函数:

usort($ztest, function($a, $b) { return strcmp($a['id'], $b['id']); });