dpqy77560 2017-06-07 13:52
浏览 60
已采纳

从多维数组中的第3级抓取所有值

In a PHP script that I'm writing I need to extract some massive JSON files and parse through them to retrieve a single value for each top level object.

My JSON file looks something like this:

[
    {
        "SOMEDATA": "SOMEVALUE",
        "MOREDATA": "MOREVALUES",
        "METADATA": {
            "CUSTOM": {
                "TYPE": "THE VALUE THAT I NEED",
                //...more irrelevant data
            },
            //...more irrelevant data
        },
        //...more irrelevant data
    },
    ... Repeats several thousand times. 
]

The only piece of data that I want right now is the TYPE, which is buried several layers down. What I want to know is if there's a way that I can extract all of those TYPEs into an array using some built in PHP functionality. I've been doing it the long way of just looping through every top level index in the JSON file, but I feel like there's probably a better way to do this. Right now I'm bench marking at over a minute for execution, which is a little painful since this is for a small web application that will be getting accessed frequently.

If there is no built in functions that I can use to achieve this result of:

[
    0 => "TYPE1",
    1 => "TYPE2",
    2 => "TYPE3",
    3 => "TYPE4",
    ...
]

is there some internal pointers or anything to PHP objects I can use to do this faster than just looping through the array?

I would normally just write the parsed array to a new file as a cron job every so often, and just grab data from the already parsed file, which would be much faster, but the JSON file changes too frequently to be able to justify this approach.

Note: I could do this with array_walk and form the new array in the callback, as suggested by some other answers, but internally that's still a loop. I want to know if there's a way to do this that in the backend takes advantage of pointer and array internals to optimize the performance. Unless in an interpreted language that isn't possible, I don't know how much PHP is able to optimize code on the fly. I'm relatively new to PHP, so I'm unsure what it's capable of.

  • 写回答

1条回答 默认 最新

  • douan3414 2017-06-07 14:06
    关注

    Assuming you're taking your JSON, running it through json_decode($json) and then using foreach on the output, that's really the best you can do. Foreach is faster than for when dealing with an array, it's just a question of how optimised the loop itself is and the hardware you're running it on at that point.

    For a simple loop like this one

    foreach($json as $val){
        $output[] = $val['metadata']['custom']['type'];
    }
    

    is about as efficient as a loop can ever be.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭