我如何将mysql结果(从mysql_fetch_array)转换为这样的形式?
$some = array(
"comments" => array(
array( "text" => "hi", "id" => "1" ),
array( "text" => "hi", "id" => "2" ),
array( "text" => "hi", "id" => "3" ),
array( "text" => "hi", "id" => "4" )
)
);
Run Code Online (Sandbox Code Playgroud)
db看起来像:
评论
id text
1 blabla bla
2 bla bla
Run Code Online (Sandbox Code Playgroud)
我试图用foreach/while获取值并将其插入两个数组但没有成功...
$some = array(
"comments" => array()
);
$q = $mysql->sf('*', TBL_QST);
foreach($mysql->fetch($q) as $row) {
$some[] = $row;
// $some["comments"][] = $row;
}
Run Code Online (Sandbox Code Playgroud)
我的print_r给出:
Array
(
[comments] => Array
(
[0] => Array
(
[text] => lorem ipsum
[id] => 0
)
[1] => Array
(
[text] => lorem ipsum
[id] => 1
)
[2] => Array
(
[text] => lorem ipsum
[id] => 2
)
)
)
Run Code Online (Sandbox Code Playgroud)
我为它做的php是:
$comments = array(
'comments' => array()
);
/* To simualate the loop */
for ($i = 0; $i < 3; $i++) {
array_push($comments['comments'], array(
'text' => 'lorem ipsum',
'id' => $i
)
);
}
Run Code Online (Sandbox Code Playgroud)
我希望这会有任何帮助,而且我没有误解你的问题.
| 归档时间: |
|
| 查看次数: |
27142 次 |
| 最近记录: |