duangouyan3328 2017-03-01 16:26
浏览 15

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

I have array of categories like this and I want to merge the same value like 1 and 0 index value etc in another new array how it is possible.

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
                    )

            )

    )
)

I want to 0 index value merge with 4 index value and get on 0 index of new array and 1 to 3 index value merge and get on 1 index in new array using php. I need help for this. thanks.

  • 写回答

1条回答 默认 最新

  • doutuo1908 2017-03-01 16:37
    关注

    It should help you:

    <?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;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了