Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign values in JSON array to variables in PHP

Tags:

json

arrays

php

I have a query that upon being JSON encoded, and print_r() looks like this:

Array
(    
    [response] => Array
        (
            [numFound] => 2392
            [start] => 0
            [maxScore] => 1
            [docs] => Array
                (
                )

        )

    [stats] => Array
        (
            [stats_fields] => Array
                (
                    [q_one] => Array
                        (
                            [min] => 0.0059
                            [max] => 6.0095
                            [count] => 2392
                            [missing] => 0
                            [sum] => 53.9131
                            [sumOfSquares] => 48.16448107
                            [mean] => 0.022538921404682
                            [stddev] => 0.14012800795752
                            [facets] => Array
                                (
                                )

                        )

                    [q_two] => Array
                        (
                            [min] => 0
                            [max] => 0.0075
                            [count] => 2392
                            [missing] => 0
                            [sum] => 17.67
                            [sumOfSquares] => 0.132525
                            [mean] => 0.0073871237458194
                            [stddev] => 0.00091333432809989
                            [facets] => Array
                                (
                                )

                        )

                    [q_three] => Array
                        (
                            [min] => 0
                            [max] => 0.065
                            [count] => 2392
                            [missing] => 0
                            [sum] => 153.14
                            [sumOfSquares] => 9.9541
                            [mean] => 0.064021739130435
                            [stddev] => 0.0079155641768643
                            [facets] => Array
                                (
                                )

                        )

                )

        )

)

Within stats >> stats_fields I want to write a foreach loop that captures the names of each header: q_one, q_two, and q_three, but I'm not sure how to do that.

I can capture the elements inside each of these 3 'headers', because their names are static, (e.g. - each one has a min and a max element)

foreach ($json['stats']['stats_fields'] as $j) {
  echo $j['min']
}

but I'm not sure how to get the header names...

like image 700
Brian Powell Avatar asked May 15 '26 23:05

Brian Powell


1 Answers

Get them in an array with array_keys():

$headers = array_keys($json['stats']['stats_fields']);

Or get them in the loop if you need it there using $key => $value syntax:

foreach ($json['stats']['stats_fields'] as $key => $j) {
  echo $key;
  echo $j['min'];
}
like image 175
AbraCadaver Avatar answered May 18 '26 11:05

AbraCadaver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!