Reputation: 147
I'm trying to convert my newsfeed result into an array, so that I can now group the results and have entries like "Peter and 6 others commented…"
I thought asking for other people, who commented on the same thread via in_array(); would be a good solution. But now I'm having a hard time converting my whole mysql result into an array without having to change my entire structure of the original while loop, where all the database values are requested with the $getnews_row. I think there might be a mistake in the way that I'm creating the arrays.
I spent the past hour googling my hands off, but I can't find any examples where a variable(array) is actually added to a multidimensional array.
Any ideas?
My PHP:
$x = 0;
while($getnews_row = mysql_fetch_array($res_getnews))
{
$this_getnewsrow = array
(
[$x] => array
($getnews_row)
)
$x++;
}
$x = $x_total;
$x=0;
while($x < $x_total)
{
$getnews_row = $this_getnewsrow[$x];
echo $getnews_row['id'];
x++;
}
My Output:
Parse error: syntax error, unexpected '[', expecting ')' in /newsfeed/index.php on line 48 (line 6 in this excerpt)
Upvotes: 0
Views: 336
Reputation: 744
You can't initialize array this way.
Try replacing this
$x = 0;
while($getnews_row = mysql_fetch_array($res_getnews))
{
$this_getnewsrow = array
(
[$x] => array
($getnews_row)
)
$x++;
}
with this
while($getnews_row = mysql_fetch_array($res_getnews))
{
$this_getnewsrow[] = $getnews_row;
}
Take a look at PHP: Arrays
Also try to replace $x = $x_total;
with $x_total = $x;
And add $
to x
variable in the second loop.
Upvotes: 2
Reputation: 13257
Try this:
$i = 0;
while($getnews_row = mysql_fetch_array ($res_getnews)) {
$this_getnewsrow [$i] = $getnews_row;
$i++;
}
$j = 0;
while($j < $i_total)
{
$getnews_row = $this_getnewsrow[$j];
echo $getnews_row ['id'];
$j++;
}
UPDATE
<?php
while ($row = mysql_fetch_array($res_getnews, MYSQL_ASSOC)) {
echo $row ['id'];
}
Upvotes: 0