简体   繁体   中英

How to access JSON element using regex or array index in PHP

I am trying to read JSON elements "unit" and "value" using PHP. However, some JSON elements ("local/sysbench-cpu-1.0.0", "m4.4xlarge-4.4.0-66-generic") may not be the same in each JSON. That is why I want to use regex or the array index to access elements rather then a particular string:

{
    "title": "cputests-sysbench-cpu-100-m4-4xlarge-20170328",
    "results": {
        "local\/sysbench-cpu-1.0.0": {
            "arguments": "cpu performance benchmark",
            "units": "seconds",
            "results": {
                "m4.4xlarge-4.4.0-66-generic": {
                    "value": "53.2386"
                }
            }
        },
        "": {
            "arguments": "Memory Usage Monitor",
            "units": "Megabytes",
            "results": {
                "m4.4xlarge-4.4.0-66-generic": {
                    "value": "1355,1356,1357,1357,1358,1359,1358,1359,1359,1358,1357,1358,1369,1370,1374,1373,1374,1376,1370,1362,1359,1360,1358,1358,1357,1358,1360,1360,1359,1359,1362,1362,1362,1363,1362,1363,1366,1365,1369,1366,1365,1363,1362,1363,1362,1363,1363,1363,1368,1374,1373,1372,1372,1373"
                }
            }
        }
    }
}

The PHP script works if I don't use regex or array index:

<?php 
$string = file_get_contents("result.json"); <br>
$data = json_decode($string); <br>
//$data = json_decode($string, true);

//var_dump(json_decode($string)); <br>


print $data->{'results'}->{**'local/sysbench-cpu-1.0.0'**}->units; <br>
print "\n";<br>
print $data->{'results'}->{'local/sysbench-cpu-1.0.0'}->{'results'}->{'**m4.4xlarge-4.4.0-66-generic**'}->value; <br>

// print $data['results']['local/sysbench-cpu-1.0.0']['units']; <br>
// print "\n"; <br>
// print $data['results']['local/sysbench-cpu-1.0.0']['results']['m4.4xlarge-4.4.0-66-generic']['value']; <br>
?>

Any attempt to use regex in place of a string or array index when using json_decode($string, true) fails.

PHP's JSON parsing only returns a plain old associative array, or an object. Neither of these support using regexes as lookups. If you insist on using native PHP, you'll have to foreach your way through the keys and values.

Or, you might take a look at something like JsonPath which allows you to load up JSON, and then use a query string that looks very close to shell globbing:

$data = ['people' => [['name' => 'Joe'], ['name' => 'Jane'], ['name' => 'John']]];
$result = (new JSONPath($data))->find('$.people.*.name'); // returns new JSONPath

So in your case, your find() string would be something like $.results.*.units for the unit values, and $.results.*.results.*.value for the values. There's probably an even fancier query string that would return both units and values at the same time.

You can use a foreach loop to go over the results ...

foreach ($json->results as $key => $results) {
  ...
  // access to $results->units

  foreach ($results as $x => $item) {
    ...
    // access to $item->value
  }
}

Then it won't matter what they key is. Something along those lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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