3

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

1 Answer 1

5

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'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.