How can i convert mysql results (from mysql_fetch_array) into such a form?
$some = array(
"comments" => array(
array( "text" => "hi", "id" => "1" ),
array( "text" => "hi", "id" => "2" ),
array( "text" => "hi", "id" => "3" ),
array( "text" => "hi", "id" => "4" )
)
);
while the db looks like:
comments
id text
1 blabla bla
2 bla bla
i've tried to fetch the values with foreach/while and insert it into two arrays but no
success...
$some = array(
"comments" => array()
);
$q = $mysql->sf('*', TBL_QST);
foreach($mysql->fetch($q) as $row) {
$some[] = $row;
// $some["comments"][] = $row;
}
解决方案
My print_r gives:
Array
(
[comments] => Array
(
[0] => Array
(
[text] => lorem ipsum
[id] => 0
)
[1] => Array
(
[text] => lorem ipsum
[id] => 1
)
[2] => Array
(
[text] => lorem ipsum
[id] => 2
)
)
)
And the php I did for it is:
$comments = array(
'comments' => array()
);
/* To simualate the loop */
for ($i = 0; $i < 3; $i++) {
array_push($comments['comments'], array(
'text' => 'lorem ipsum',
'id' => $i
)
);
}
I hope this will be to any help and that I didn't misunderstood your question.