dosf40815 2016-10-25 17:22 采纳率: 100%
浏览 47
已采纳

如何从多维数组中提取特定的关键数据

I'm converting XML file into associative array to pull out the data, the problem is that I have to make 10 loops depends on arrays number in order to get the data.

is there a better way to get a specific column data without creating many loops? because I want to assign them to variables.

the array I'm trying to get data from

Array
(
    [catalog] => Array
        (
            [comp] => Array
                (
                    [0] => Array
                        (
                            [look] => Array
                                (
                                    [shp] => Array
                                        (
                                            [wok] => Array
                                                (
                                                    [group] => Array
                                                        (
                                                            [customer] => Array
                                                                (
                                                                    [author] => jack
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                            [customer2] => Array
                                                                (
                                                                    [author] => lemass
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [look] => Array
                                (
                                    [shp] => Array
                                        (
                                            [wok] => Array
                                                (
                                                    [group] => Array
                                                        (
                                                            [customer] => Array
                                                                (
                                                                    [author] => jon
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                            [customer2] => Array
                                                                (
                                                                    [author] => kirito
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

I'm trying to get the data like this!

I have 2 arrays "customer" and "customer1"

I want to get the data like this

customer=>author

the output

jack
jon

because they are in the customer array

its it possible to do that ??

  • 写回答

2条回答 默认 最新

  • dongpian6319 2016-10-25 17:34
    关注

    Say your array is stored in $arr you would access the comp index and then loop it since those are numeric indexes. Then you have an array to whittle down some more. This all seems a bit bloated by the array structure but will work

    $arr; //Set this to your converted xml
    $comps = $arr['catalog']['comp'];
    
    foreach($comps as $comp){
        echo $comp['look']['shp']['wok']['group']['customer']['author'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler