0

I'm looking to manipulate an array that was generated from a MySQL query. The print_r format returns the following:

Array ( [cols] => Array ( [0] => Array ( [label] => time [type] => number ) [1] => Array ( [label] => quantity [type] => number ) ) [rows] => Array ( [0] => Array ( [c] => Array ( [0] => Array ( [v] => 8.8 ) [1] => Array ( [v] => 3 ) ) ) [1] => Array ( [c] => Array ( [0] => Array ( [v] => 8.2 ) [1] => Array ( [v] => 4 ) ) ) [2] => Array ( [c] => Array ( [0] => Array ( [v] => 7.3 ) [1] => Array ( [v] => 1 ) ) ) [3] => Array ( [c] => Array ( [0] => Array ( [v] => 5.7 ) [1] => Array ( [v] => 3 ) ) ) [4] => Array ( [c] => Array ( [0] => Array ( [v] => 4.9 ) [1] => Array ( [v] => 2 ) ) ) [5] => Array ( [c] => Array ( [0] => Array ( [v] => 2.9 ) [1] => Array ( [v] => 1 ) ) ) [6] => Array ( [c] => Array ( [0] => Array ( [v] => 1.6 ) [1] => Array ( [v] => 1 ) ) ) ) )

I put this output into the array beautifier here in order to better understand the structure: http://phillihp.com/toolz/php-array-beautifier/

However, even with this prettier version, I'm unsure how to access specific elements. For example, how would I return just the pair of "8.2","4" bolded above from this array? Any insight into this structure would be greatly appreciated.

Here's the code that generated this array:

$return_structure = array(
    'cols' => array (
       // array('label' => 'name', 'type' => 'string'),
        array('label' => 'time', 'type' => 'number'),
        array('label' => 'quantity', 'type' => 'number')
    ),
    'rows' => array()
);

while($row = mysql_fetch_assoc($result)) {
        $return_structure['rows'][] = array('c' => array(
        //array('v' => $row['name']),
        array('v' => $row['time']),
        array('v' => $row['quantity']),
            ));

Thanks in advance for any assistance!! -Daniel

5
  • 4
    Can we see how you are retrieving your data with the PHP and the SQL Query? Commented Apr 1, 2014 at 21:20
  • $return_structure = array( 'cols' => array ( // array('label' => 'name', 'type' => 'string'), array('label' => 'time', 'type' => 'number'), array('label' => 'quantity', 'type' => 'number') ), 'rows' => array() ); while($row = mysql_fetch_assoc($result)) { $return_structure['rows'][] = array('c' => array( //array('v' => $row['name']), array('v' => $row['time']), array('v' => $row['quantity']), )); Commented Apr 1, 2014 at 21:22
  • ummmm sorry for the poor format in the comment, I'm not sure how to paste in clean code blocks here thank you for the quick response Commented Apr 1, 2014 at 21:23
  • Just edit your question, and I am looking for the mysql_query or whatever you used to retrieve the data from the database Commented Apr 1, 2014 at 21:24
  • try printing inside of a <pre> tag if you're debugging with prints. It should keep whitespace and print_r will have correct formatting. Commented Apr 1, 2014 at 21:25

1 Answer 1

2

A good way to get a "pretty" view quickly is to view it in your web browser using the html "pre" tag:

echo "<pre>";
print_r($array);

When accessing a multi dimensional associative array, you can either:

1) use a loop to go through everything, and you may need nested loops to accomplish this. Even using a bunch of foreach loops this could get quite messy and cumbersome.

something like:

foreach ($arrayDImension as $subArray) {
    foreach($subArray as $row -> $value) {
      //access the rows & values here
    }
}

OR

2) access them directly , assuming you know the names of the indexes, etc. You could access them like this:

print_r($array['rows'][3]['c'][0]['v']); // gives you 8.2

print_r($array['rows'][4]['c'][5]['v']); //gives you 4
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.