使用php从三维JSON中删除元素

使用php从三维JSON中删除元素,php,json,multidimensional-array,Php,Json,Multidimensional Array,这是我的JSON: { "status":"ok", "status_message":"Query was successful", "data":{ "movie_count":3, "limit":20, "page_number":1, "movies":[ { "id":5039, "url":"https:\/\/yts.ag\/movie\/steve

这是我的JSON:

{
   "status":"ok",
   "status_message":"Query was successful",
   "data":{
      "movie_count":3,
      "limit":20,
      "page_number":1,
      "movies":[
         {
            "id":5039,
            "url":"https:\/\/yts.ag\/movie\/steve-jobs-2015",
            "imdb_code":"tt2080374",
            "title":"Steve Jobs",
            "title_english":"Steve Jobs",
            "title_long":"Steve Jobs (2015)",
            "slug":"steve-jobs-2015",
            "year":2015,
            "rating":7.3,
            "runtime":122,
            "genres":[
               "Biography",
               "Drama"
            ],
            "summary":"His passion and ingenuity have been the driving force behind the digital age. However his drive to revolutionize technology was sacrificial. Ultimately it affected his family life and possibly his health. In this revealing film we explore the trials and triumphs of a modern day genius, the late CEO of Apple inc. Steven Paul Jobs.",
            "description_full":"His passion and ingenuity have been the driving force behind the digital age. However his drive to revolutionize technology was sacrificial. Ultimately it affected his family life and possibly his health. In this revealing film we explore the trials and triumphs of a modern day genius, the late CEO of Apple inc. Steven Paul Jobs.",
            "synopsis":"His passion and ingenuity have been the driving force behind the digital age. However his drive to revolutionize technology was sacrificial. Ultimately it affected his family life and possibly his health. In this revealing film we explore the trials and triumphs of a modern day genius, the late CEO of Apple inc. Steven Paul Jobs.",
            "yt_trailer_code":"aEr6K1bwIVs",
            "language":"English",
            "mpa_rating":"R",
            "background_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_2015\/background.jpg",
            "background_image_original":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_2015\/background.jpg",
            "small_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_2015\/small-cover.jpg",
            "medium_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_2015\/medium-cover.jpg",
            "large_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_2015\/large-cover.jpg",
            "state":"ok",
            "torrents":[
               {
                  "url":"https:\/\/yts.ag\/torrent\/download\/88208992C5CA27CAF4F9AC2B8F2908F548D8082A.torrent",
                  "hash":"88208992C5CA27CAF4F9AC2B8F2908F548D8082A",
                  "quality":"720p",
                  "seeds":247,
                  "peers":31,
                  "size":"904.98 MB",
                  "size_bytes":948940308,
                  "date_uploaded":"2016-02-03 19:22:46",
                  "date_uploaded_unix":1454545366
               },
               {
                  "url":"https:\/\/yts.ag\/torrent\/download\/46E7E399850BDC9039182074EFA32957CD82C9F1.torrent",
                  "hash":"46E7E399850BDC9039182074EFA32957CD82C9F1",
                  "quality":"1080p",
                  "seeds":273,
                  "peers":35,
                  "size":"1.87 GB",
                  "size_bytes":2007897211,
                  "date_uploaded":"2016-02-03 23:05:02",
                  "date_uploaded_unix":1454558702
               }
            ],
            "date_uploaded":"2016-02-03 19:22:46",
            "date_uploaded_unix":1454545366
         },
         {
            "id":4731,
            "url":"https:\/\/yts.ag\/movie\/steve-jobs-the-man-in-the-machine-2015",
            "imdb_code":"tt4425064",
            "title":"Steve Jobs: The Man in the Machine",
            "title_english":"Steve Jobs: The Man in the Machine",
            "title_long":"Steve Jobs: The Man in the Machine (2015)",
            "slug":"steve-jobs-the-man-in-the-machine-2015",
            "year":2015,
            "rating":6.8,
            "runtime":128,
            "genres":[
               "Documentary"
            ],
            "summary":"In his signature black turtleneck and blue jeans, shrouded in shadows below a milky apple, Steve Jobs' image was ubiquitous. But who was the man on the stage? What accounted for the grief of so many across the world when he died? From Oscar-winning director Alex Gibney, 'Steve Jobs: The Man In The Machine' is a critical examination of Jobs who was at once revered as an iconoclastic genius and a barbed-tongued tyrant. A candid look at Jobs' legacy featuring interviews with a handful of those close to him at different stages in his life, the film is evocative and nuanced in capturing the essence of the Apple legend and his values which shape the culture of Silicon Valley to this day.",
            "description_full":"In his signature black turtleneck and blue jeans, shrouded in shadows below a milky apple, Steve Jobs' image was ubiquitous. But who was the man on the stage? What accounted for the grief of so many across the world when he died? From Oscar-winning director Alex Gibney, 'Steve Jobs: The Man In The Machine' is a critical examination of Jobs who was at once revered as an iconoclastic genius and a barbed-tongued tyrant. A candid look at Jobs' legacy featuring interviews with a handful of those close to him at different stages in his life, the film is evocative and nuanced in capturing the essence of the Apple legend and his values which shape the culture of Silicon Valley to this day.",
            "synopsis":"In his signature black turtleneck and blue jeans, shrouded in shadows below a milky apple, Steve Jobs' image was ubiquitous. But who was the man on the stage? What accounted for the grief of so many across the world when he died? From Oscar-winning director Alex Gibney, 'Steve Jobs: The Man In The Machine' is a critical examination of Jobs who was at once revered as an iconoclastic genius and a barbed-tongued tyrant. A candid look at Jobs' legacy featuring interviews with a handful of those close to him at different stages in his life, the film is evocative and nuanced in capturing the essence of the Apple legend and his values which shape the culture of Silicon Valley to this day.",
            "yt_trailer_code":"SrlPyKxdMX4",
            "language":"English",
            "mpa_rating":"R",
            "background_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_the_man_in_the_machine_2015\/background.jpg",
            "background_image_original":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_the_man_in_the_machine_2015\/background.jpg",
            "small_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_the_man_in_the_machine_2015\/small-cover.jpg",
            "medium_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_the_man_in_the_machine_2015\/medium-cover.jpg",
            "large_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/steve_jobs_the_man_in_the_machine_2015\/large-cover.jpg",
            "state":"ok",
            "torrents":[
               {
                  "url":"https:\/\/yts.ag\/torrent\/download\/C04D9D0C73281EE708C5A7DD44366B77C54CD722.torrent",
                  "hash":"C04D9D0C73281EE708C5A7DD44366B77C54CD722",
                  "quality":"720p",
                  "seeds":23,
                  "peers":4,
                  "size":"692.27 MB",
                  "size_bytes":725897708,
                  "date_uploaded":"2015-11-20 16:19:01",
                  "date_uploaded_unix":1448054341
               },
               {
                  "url":"https:\/\/yts.ag\/torrent\/download\/4B373BB0E4AFEA0467E87EED686D9A2458D41CC3.torrent",
                  "hash":"4B373BB0E4AFEA0467E87EED686D9A2458D41CC3",
                  "quality":"1080p",
                  "seeds":17,
                  "peers":2,
                  "size":"1.31 GB",
                  "size_bytes":1406601789,
                  "date_uploaded":"2015-11-20 16:19:08",
                  "date_uploaded_unix":1448054348
               }
            ],
            "date_uploaded":"2015-11-20 16:19:01",
            "date_uploaded_unix":1448054341
         },
         {
            "id":1699,
            "url":"https:\/\/yts.ag\/movie\/jobs-2013",
            "imdb_code":"tt2357129",
            "title":"Jobs",
            "title_english":"Jobs",
            "title_long":"Jobs (2013)",
            "slug":"jobs-2013",
            "year":2013,
            "rating":5.9,
            "runtime":128,
            "genres":[
               "Biography",
               "Drama"
            ],
            "summary":"The story of Steve Jobs' ascension from college dropout into one of the most revered creative entrepreneurs of the 20th century.",
            "description_full":"The story of Steve Jobs' ascension from college dropout into one of the most revered creative entrepreneurs of the 20th century.",
            "synopsis":"The story of Steve Jobs' ascension from college dropout into one of the most revered creative entrepreneurs of the 20th century.",
            "yt_trailer_code":"FrvkCS0ZGPU",
            "language":"English",
            "mpa_rating":"PG-13",
            "background_image":"https:\/\/yts.ag\/assets\/images\/movies\/JOBS_2013\/background.jpg",
            "background_image_original":"https:\/\/yts.ag\/assets\/images\/movies\/JOBS_2013\/background.jpg",
            "small_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/JOBS_2013\/small-cover.jpg",
            "medium_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/JOBS_2013\/medium-cover.jpg",
            "large_cover_image":"https:\/\/yts.ag\/assets\/images\/movies\/JOBS_2013\/large-cover.jpg",
            "state":"ok",
            "torrents":[
               {
                  "url":"https:\/\/yts.ag\/torrent\/download\/7BB7D4B9AF75FCA9385C02B2A031950C5F05F36F.torrent",
                  "hash":"7BB7D4B9AF75FCA9385C02B2A031950C5F05F36F",
                  "quality":"720p",
                  "seeds":63,
                  "peers":6,
                  "size":"921.77 MB",
                  "size_bytes":966545900,
                  "date_uploaded":"2015-11-01 00:25:49",
                  "date_uploaded_unix":1446351949
               },
               {
                  "url":"https:\/\/yts.ag\/torrent\/download\/1E82235C55769328435AD1AC69D05D1E5D741B9C.torrent",
                  "hash":"1E82235C55769328435AD1AC69D05D1E5D741B9C",
                  "quality":"1080p",
                  "seeds":29,
                  "peers":8,
                  "size":"1.95 GB",
                  "size_bytes":2093796557,
                  "date_uploaded":"2015-11-01 00:25:52",
                  "date_uploaded_unix":1446351952
               }
            ],
            "date_uploaded":"2015-11-01 00:25:49",
            "date_uploaded_unix":1446351949
         }
      ]
   },
   "@meta":{
      "server_time":1461011131,
      "server_timezone":"EST5EDT",
      "api_version":2,
      "execution_time":"0.01 ms"
   }
}
我需要做的是从“Torrents”项目中删除所有具有“quality=720p”的项目

我试过这样的方法:

    $data= json_decode($a, true);
    //Loop through the array contained in the 'torrents' object.
    foreach ($data['data']['movies'] AS $key => $value) {
       // Assess the value of the 'quality' object
            foreach ($value['torrents'] AS $key2 => $value2) {
               if ($value2['quality'] == "720p"){
                    echo $value2['quality'];
                    unset($value['torrents'][$key2]);
               }
            }
    }
    $data= json_encode($data);
    echo $data;
但是由于这个JSON有两个以上的维度,我不知道如何使它工作

我该怎么做


关于,

数组中包含720p的部分实际上是{}定义的对象,而数组是[]。应该做的是:

$data= json_decode($json, true); 
//Loop through the array contained in the 'torrents' object.
foreach ($data->torrents AS $value) {
   // Assess the value of the 'quality' object
   if ($value->quality == "720p"){
    unset($value->quality);
   }
}

另外,我建议您将来简化示例数据,并正确设置格式以便于阅读

看。顺便说一句:这里没有json脚本。这只是JSON,“过滤器”这个词并没有告诉我们你想做什么。这个?所以你问这个网站如何盗版质量更好的电影?不,我问这个网站如何用PHP从JSON中删除项目。谢谢道格,我试过你的例子,但没有成功。我收到“警告:为foreach()提供的参数无效”。B顺便说一句,很抱歉,我以后会更加小心。你能接受我的编辑建议吗。在我的手机上,无法按原样读取Json。完成了,我不知道你可以这么做。谢谢啊哈,比我注意到的还要深。你需要先循环$data->movies,然后在循环中循环torrents。之前我可以这样做(删除了我的代码),但我无法取消设置spesific元素,然后编码新的JSON。