PHP合并一个多维数组';让孩子们进入另一个网络

PHP合并一个多维数组';让孩子们进入另一个网络,php,arrays,Php,Arrays,我已经爬了好几个小时,试图找到解决这个问题的办法,但我做不到 情况:假设您正在设置一个多维数组,并且希望使用一个返回多维数组的函数来设置它的一些值 挑战在于,由于使用该函数返回多个数组,因此基本上只需获取返回数组的子数组并将其插入父级。你是做什么的 下面是一些示例代码: function some_function(){ $items = array(); $items[] = array( 'id' => 'thing2',

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

情况:假设您正在设置一个多维数组,并且希望使用一个返回多维数组的函数来设置它的一些值

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

下面是一些示例代码:

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);
   <?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);
    ?>
我希望得到的结果是:

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
    )
)
我尝试了数组映射、数组过滤器、数组值、数组合并。。。似乎什么都不管用

这似乎很简单,我觉得应该可以做到,但我想不出来

注意:我知道我可以在事后将这两个数组合并,或者在事后插入它,但是为了我正在处理的项目中的代码整洁,我更喜欢内联执行

多谢各位

-Zach

试试这个功能:
array\u merge\u recursive
以下是示例代码:

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);
   <?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);
    ?>

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


嗨,雷,非常感谢你花时间回复。是的,这肯定会起作用,但我想知道的是,是否有一种很好的方法可以在线完成它,如我上面所述。我在这个项目中使用的“真实”代码使得按照您在这里概述的方式来做有点杂乱和笨拙。你知道怎么做吗?我编辑了代码,删除了包含第二个数组的变量,让数组将它们合并在一行中,我想这是最好的方法,需要你多加一行来完成。这是你需要添加到代码中的唯一一行,
$result=array\u merge\u recursive($ztest,some\u function());在实际部署中,并不是这么简单。数组嵌套在一个大型类声明中,并且还需要按特定顺序输出,因为它们用于wordpress选项菜单框架,该框架只运行选项列表。因此,您在这里概述的方法并不理想。Laravel将其作为自定义函数编写,然后将其包含在他们的框架中,但PHP没有直接的函数来实现这一点,谢谢,u_mulder。这当然有道理,但您不知道有没有一种方法可以直接插入另一个数组的子数组,而不直接使用array_merge和usort?否。您还可以使用
foreach
迭代一个数组,并使用
[]
将此数组的每个项添加到新数组中。