4

Edit: Nico Parodi's answer is correct. I will eventually return to finding out why, but for now I will just take it as it is and hope nothing else fails.

I have a table with three fields: "date", "name", "location". I want to group all the records selected from this table based on their date.

By copy-pasting some code from php mysql group by date with yyyy-mm-dd format, I've managed to get this array, date -> name:

$result = mysqli_query($con,"SELECT date, name, location FROM events");
while($row = mysqli_fetch_array($result)) {
    $foo[$row['date']][]=$row['name'];
}

Everything's great, I can iterate it without issues. Now I want to store all the row columns as a value for the date key, so I try to store the entire row as a value:

$result = mysqli_query($con,"SELECT date, name, location FROM events");
while($row = mysqli_fetch_array($result)) {
    $foo[$row['date']][]=$row;
}

And now I can't iterate it. count($rowCol) seems to give me the nr of columns in all the array for either keys, not just for one. How can I iterate it?

foreach($foo as $date => $events) {
    echo $date . ": "; // this is okay         

    foreach($events as $key => $rowCol){
        for ($i = 0; $i<count($rowCol); $i++) {
            echo $rowCol[$i] . " ";     
        }
    }
}
4
  • Please edit, I can't see it - this is a simplified version of my code. The actual one compiles/runs fine. Commented Jun 18, 2013 at 18:59
  • @Buffalo Here: $row['date'])] Commented Jun 18, 2013 at 18:59
  • Yes, good spot! I had a substr() call there. Commented Jun 18, 2013 at 19:02
  • use var_dump($events); under echo $date . ":"; and post the output Commented Jun 18, 2013 at 19:08

4 Answers 4

1
for ($i = 0; $i<count($rowCol); $i++) {
    echo $rowCol[$i] . " ";     
}

$rowCol has the double of fields that you expect as mysqli_fetch_array() fetches as both associative and numeric array. So,

for ($i = 0; $i<(count($rowCol)/2); $i++) {
        echo $rowCol[$i] . " ";     
    }

should work.

Sign up to request clarification or add additional context in comments.

4 Comments

Because mysqli_fetch_row() fetches only as numeric array so you now have the number of fields you always expected, so in this case you don't have to use count($rowCol)/2, just use as before, count($rowCol).
use var_dump($rowCol) to print the array and see why it has the double of fields
Minor question: can I also get the key when I'm iterating? the name of the table column?
yes, to do it you should change mysqli_fetch_array call to mysqli_fetch_assoc to fetch results into only associative array - and then you can use foreach ($rowCol as $colName=>$val)
1

Replace :

    for ($i = 0; $i<count($rowCol); $i++) {
        echo $rowCol[$i] . " ";     
    }

By

    foreach($rowCol as $k =>$v) {
        echo $v . " ";     
    }

Comments

1
for ($i = 0; $i<count($rowCol); $i++) {
        echo $rowCol[$i] . " ";     
    }

this part of code doesn't work because rowCol is an associative array + numeric array. Try to replace mysqli_fetch_array with mysqli_fetch_row

2 Comments

Mind posting the appropriate foreach? believe me, I've tried to no avail.
update: my fault, try to replace mysqli_fetch_array with mysqli_fetch_row
1

Can you tell us what's the output of this code? (show plain text, no HTML):

foreach($foo as $date => $event) {
    echo $date . ": ";

    foreach($event as $key => $value){
        print_r($value);
    }

    echo "\n";
}

Original answer:

$events contains a single event, your code should look more like this:

foreach($foo as $date => $event) {
    echo $date . ": ";

    foreach($event as $key => $value){
        echo $value . " ";
    }

    echo "\n";
}

1 Comment

This prints out "2001-02: Array 2012-02: Array 2012-03: Array 2013-03: Array Array".

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.