This might be quite a trivial question, but which of these methods is best practice for structuring an array for return db results? say a list of blog posts... Sorting and grouping posts? or sorting and grouping elements?
Array
(
[title] => Array
(
[0] => Untitled
[1] => Untitled
)
[id] => Array
(
[0] => 8
[1] => 11
)
)
or
Array
(
[0] => Array
(
['id']=> 8
['title'] => Untitled
)
[1] => Array
(
['id']=> 11
['title'] => Untitled
)
)
The first way seems the easiest and the way I have been doing it. I can simply:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$post['title'][] = $row['title'];
$post['id'][] = $row['id'];
}
and then
$count = count($post['id']);
but the second way seems to make better sense and structure the information more logically. It is just a little more complicated to set up, filter and work with in the template.
$c = 0;
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$post[$c]['title'] = $row['title'];
$post[$c]['id'] = $row['id'];
$c++;
}