mysql取出全部数据存入数组,Mysql的结果存入数组(PHP)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值