Mysql的结果存入数组(PHP) [英] Mysql results into array (PHP)

查看:365
本文介绍了Mysql的结果存入数组(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换mysql的结果(从mysql_fetch_array)成这样的形式?

  $ =一些阵列(
     意见=>阵列(
         阵列(文本=>中。在,ID=>中1),
         阵列(文字=>中喜,ID=>2),
         阵列(文本=>中。在,ID=>中3),
         阵列(文字=>中喜,ID=>4)
    )
);

而DB如下:

注释

  ID文本
1 BLA布拉布拉
2喇嘛喇嘛

我已经试过用foreach获取的值/时,将其插入两个数组,但没有
成功...

  $ =一些阵列(
    意见=>阵列()
);$ Q = $ mysql-> SF(*,TBL_QST);
的foreach($ mysql->取($ Q)为$行){
    一些$ [] = $行;
    // $一些[意见] [] = $行;
}


解决方案

我的print_r给出了:

 阵列

    [评论] =>排列
        (
            [0] =>排列
                (
                    [文] => Lorem存有
                    [ID] => 0
                )
            [1] =>排列
                (
                    [文] => Lorem存有
                    [ID] => 1
                )
            [2] =>排列
                (
                    [文] => Lorem存有
                    [ID] => 2
                )
        )

和我做了它的PHP是:

  $意见=阵列(
    意见=>阵列()
    );/ *要simualate循环* /
为($ I = 0; $ I 3;; $ I ++){
    array_push($评论['意见'],阵列(
        文本=> Lorem存有',
        'ID'=> $ I
        )
    );
}

我希望这将是任何人的帮助,而且我也没有误解你的问题。

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.

这篇关于Mysql的结果存入数组(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆