dsstjqsr631426 2012-07-02 06:42
浏览 18
已采纳

php中的数组管理

Its generally related to a question in magento ..but i think using core php can also give me a wat i want

Im having a looping array issue here,My aray is like this

    Array
    (
        [0] => Array
            (
                [category_id] => 2
                [parent_id] => 1
                [name] => Koffersenkisten
                [is_active] => 0
                [position] => 1
                [level] => 1
                [children] => Array
                    (
                        [0] => Array
                            (
                                [category_id] => 40
                                [parent_id] => 2
                                [name] => Muziek
                                [is_active] => 1
                                [position] => 1
                                [level] => 2
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [category_id] => 46
                                                [parent_id] => 40
                                                [name] => Gitaar koffer
                                                [is_active] => 1
                                                [position] => 1
                                                [level] => 3
                                                [children] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [category_id] => 50
                                                                [parent_id] => 46
                                                                [name] => Bas gitaar koffer
                                                                [is_active] => 1
                                                                [position] => 1
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [1] => Array
                                                            (
                                                                [category_id] => 51
                                                                [parent_id] => 46
                                                                [name] => Electrische gitaar koffer
                                                                [is_active] => 1
                                                                [position] => 2
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [2] => Array
                                                            (
                                                                [category_id] => 47
                                                                [parent_id] => 46
                                                                [name] => Akoestische gitaar koffer
                                                                [is_active] => 1
                                                                [position] => 3
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [3] => Array
                                                            (
                                                                [category_id] => 49
                                                                [parent_id] => 46
                                                                [name] => Gitaar soft cases
                                                                [is_active] => 1
                                                                [position] => 4
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [4] => Array
                                                            (
                                                                [category_id] => 52
                                                                [parent_id] => 46
                                                                [name] => Gitaar gig bags
                                                                [is_active] => 1
                                                                [position] => 5
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [5] => Array
                                                            (
                                                                [category_id] => 53
                                                                [parent_id] => 46
                                                                [name] => Pedalboards
                                                                [is_active] => 1
                                                                [position] => 6
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [6] => Array
                                                            (
                                                                [category_id] => 48
                                                                [parent_id] => 46
                                                                [name] => Amp Utility Vehicles
                                                                [is_active] => 1
                                                                [position] => 7
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

                                                    )

                                            )

                                        [1] => Array
                                            (
                                                [category_id] => 67
                                                [parent_id] => 40
                                                [name] => Percussie koffer
                                                [is_active] => 1
                                                [position] => 2
                                                [level] => 3
                                                [children] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [category_id] => 73
                                                                [parent_id] => 67
                                                                [name] => Tom koffer
                                                                [is_active] => 1
                                                                [position] => 1
                                                                [level] => 4
                                                                [children] => Array
                                                                    (
                                                                    )

                                                            )

.......


......


>> it goes like this based on the number of categories

How do i get a simple array of the above like this

array('2' => 'Koffersenkisten','40'=> 'Muziek'...........etc); where 2 is the category_id and Koffersenkisten is the name
  • 写回答

3条回答 默认 最新

  • drxkx6149 2012-07-02 07:07
    关注

    This function iterates over all items, and recurses when children are found. It passes the output array along at every invocation and appends to it:

    function get_cats(array &$output, array $arr)
    {
            foreach ($arr as $item) {
                    $output[$item['category_id']] = $item['name'];
                    if (count($item['children'])) {
                            get_cats($output, $item['children']);
                    }
            }
    }
    
    // start with empty result
    $output = array();
    get_cats($output, $a);
    print_r($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化