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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器