使用PHP中的另一个数组创建一个新数组

时间:2017-03-01 16:26:36

标签: php codeigniter

我有这样的类别数组,我想在另一个新数组中合并相同的值,如1和0索引值等,如何实现。

Array
(
    [0] => Array
        (
            [id] => 29
            [parentId] => 0
            [serviceName] => Hair
            [parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
            [subCategory] => Array
                (
                    [id] => 30
                    [parentId] => 29
                    [serviceName] => Blow Out
                    [subCategory] => Array
                        (
                            [id] => 31
                            [parentId] => 30
                            [serviceName] => Add Clients Hair Extensions & Style
                            [subCategory] => Array
                                (
                                    [id] => 49
                                    [parentId] => 31
                                    [serviceName] => Hair style color
                                    [servicePrice] => 100
                                )

) ) ) [1] => Array ( [id] => 1 [parentId] => 0 [serviceName] => Makeup [parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg [subCategory] => Array ( [id] => 2 [parentId] => 1 [serviceName] => Special Event Makeup [subCategory] => Array ( [id] => 3 [parentId] => 2 [serviceName] => Add Airbrush [servicePrice] => 232 ) ) ) [2] => Array ( [id] => 1 [parentId] => 0 [serviceName] => Makeup [parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg [subCategory] => Array ( [id] => 6 [parentId] => 1 [serviceName] => Photoshot Makeup [subCategory] => Array ( [id] => 7 [parentId] => 6 [serviceName] => Add Airbrush [servicePrice] => 8 ) ) ) [3] => Array ( [id] => 1 [parentId] => 0 [serviceName] => Makeup [parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg [subCategory] => Array ( [id] => 2 [parentId] => 1 [serviceName] => Special Event Makeup [subCategory] => Array ( [id] => 4 [parentId] => 2 [serviceName] => Add Lashes [servicePrice] => 6 ) ) ) [4] => Array ( [id] => 29 [parentId] => 0 [serviceName] => Hair [parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg [subCategory] => Array ( [id] => 2 [parentId] => 1 [serviceName] => Special Event Makeup [subCategory] => Array ( [id] => 47 [parentId] => 29 [serviceName] => Wedding Day Mother of the Bride / Mother of the Groom Hair Touch Ups [servicePrice] => 5 ) ) ) )

我希望0索引值与4个索引值合并并获得新数组的0索引和1到3索引值合并并使用php在新数组中获得1个索引。我需要帮助。感谢。

1 个答案:

答案 0 :(得分:0)

它可以帮到你:

<?php
$your_array  = [0=>['array1'], 1=>['array2'],2=>['array3'], 3=>['array4'], 4=>['array5']];
$new_array   = [];
$first       = array_shift($your_array);
$last        = array_pop($your_array);
$array       = array_merge($first, $last);
$new_array[] = $array;
$new_array[] = $your_array;
?>