2017-06-07 13:52
浏览 50


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:

        "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.

图片转代码服务由CSDN问答提供 功能建议

在我正在编写的PHP脚本中,我需要提取一些大量的JSON文件并解析它们以检索 每个顶级对象的单个值。


 // ......更无关紧要的数据
},\  n //...更多无关数据
 // ..更多无关数据

我现在想要的唯一数据是TYPE,它被埋下了几层。 我想知道的是,如果有一种方法可以使用一些内置的PHP功能将所有这些TYPE提取到一个数组中。 我一直在做的只是循环遍历JSON文件中的每个顶级索引,但我觉得可能有更好的方法来做到这一点。 现在我在执行时超过一分钟进行基准测试,这有点痛苦,因为这是一个经常被访问的小型Web应用程序。


 0 =>  “TYPE1”,
 1 =>  “TYPE2”,
 2 =>  “TYPE3”,
 3 =>  “TYPE4”,

是否有一些内部指针或PHP对象的任何内容我可以用它来做到这一点,而不仅仅是循环 阵列?

我通常只是经常将解析后的数组作为cron作业写入新文件,只是从已经解析过的文件中获取数据,这会快得多,但是JSON 文件更改太频繁,无法证明这种方法的合理性。

注意:我可以使用array_walk执行此操作,并在回调中构建新数组,如其他一些答案所示,但在内部仍然是循环。 我想知道是否有办法在后端利用指针和数组内部优势来优化性能。 除非使用不可能的解释语言,否则我不知道PHP能够动态优化代码。 我对PHP比较陌生,所以我不确定它的功能是什么。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    点赞 评论

相关推荐 更多相似问题