Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
4  
Can we see how you are retrieving your data with the PHP and the SQL Query? –  Chitowns24 Apr 1 '14 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']), )); –  DMBnyc Apr 1 '14 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 –  DMBnyc Apr 1 '14 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 –  Chitowns24 Apr 1 '14 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. –  Grashlok Apr 1 '14 at 21:25

2 Answers 2

up vote 1 down vote accepted

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
share|improve this answer

There is nothing difficult here. Please, take a look at the docs so you have a fundamental understanding on how to work with arrays.

This is how you access 8.2

$return_structure['rows'][1]['c'][0]['v']

This is how you access 4

$return_structure['rows'][1]['c'][1]['v']
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.