Quantcast
Channel: User andrewsi - Stack Overflow
Viewing all articles
Browse latest Browse all 51

Answer by andrewsi for Unsure what PHP array is doing upon assigning

$
0
0

Your two assignments are doing two subtly different things.

while ($stmt->fetch()) {
    $data[$ddp][0] = $row[0];
    $data[$ddp][1] = $row[1];
    $ddp++;
}

This creates two entries in the array - the first will have the index [0][0], and be assigned the value in $row[0], and so on.

On the other hand, this:

while ($stmt->fetch()) {
    $data[$ddp++] = $row;
    // I ECHOd out $row here... and it did fetch 2 different rows...
}

is adding a single row to the database, and adding the whole of $row to $data[0] on the first run through. So whatever is returned from the database is added as it is - if you're getting an array back from fetch(), then you're adding an array inside your array.


Viewing all articles
Browse latest Browse all 51

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>