简体   繁体   English

PHP在解码多层JSON数组时遇到麻烦

[英]PHP Having trouble decoding multilevel JSON array

I am making a cheat detecting small software piece for personal use in PHP, the code scans all files in a directory, sorts out all of the .JSON files and then will parse and return some values. 我正在作弊以检测供PHP个人使用的小型软件,代码扫描目录中的所有文件,整理出所有.JSON文件,然后解析并返回一些值。 I have one function working to tell me what the version number is from the JSON file: 我有一个函数可以告诉我JSON文件的版本号是什么:

function scan_Versions($dir) {
  $array = array();
  $rdi = new RecursiveDirectoryIterator($dir);
  foreach (new RecursiveIteratorIterator($rdi) as $filename => $file) {
    if(!is_dir($filename)) {
      $path_parts = pathinfo($filename);
      $ext = $path_parts["extension"]. PHP_EOL;
      if (strpos($ext, "json") !== false) {
        $str = file_get_contents($filename);
        $json = json_decode(utf8_decode($str), true);
        array_push($array,$json['id']);
      }
    }
  }
  return $array;
}

When executed it will scan the files, and get data from a JSON file like this: http://pastebin.com/3VdEZW43 (It gets the first id part of the JSON ) 执行后,它将扫描文件,并从JSON文件中获取数据,如下所示: http : //pastebin.com/3VdEZW43 (它获取JSON的第一个id部分)

I need a function like the one above to retrieve the data from: 我需要一个类似上面的函数来从中检索数据:

"libraries": [
    {
      "name": "oshi-project:oshi-core:1.1", <----- I NEED THIS!!!!!!!!!!
      "downloads": {
        "artifact": {
          "url": "https://libraries.[REMOVED].net/oshi-project/oshi-core/1.1/oshi-core-1.1.jar",
          "sha1": "9ddf7b048a8d701be231c0f4f95fd986198fd2d8",
          "size": 30973
        }
      }
    }.......

From the "name": that contains: oshi-project:oshi-core:1.1 . 来自"name":包含: oshi-project:oshi-core:1.1 Can someone help me do this? 有人可以帮我吗?

I don't have enough rep to add a comment (new here), but you would want 我没有足够的代表来添加评论(这里是新评论),但是您想要

$json["libraries"][0]["name"]

not

$json["libraries"]["name"][0]

according to the pastebin link. 根据pastebin链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM