将PHP结果集转换为数组 [英] Convert a php resultset into an array

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

问题描述

我正在尝试将php结果集转换为数组(使用foreach),但我做得还不够..

I am trying to convert a php resultset into an array (using foreach), I'm not quite doing it right..

我有一个结果集:

$result

我正在如下循环浏览:(非常错误)

I am looping through it as below: (horribly wrong)

while($row = mysql_fetch_array($result)) {

    foreach (mysql_fetch_array($result) as $k => $v) {
      echo "Fred's $k is ". $v['uid']. "\n";
    }

}

我希望此数组采用以下格式:

I want this array to be in the below format:

Array
(
    [0] => Array    //row1
        (
            [column1] => value
            [column2] => value
            [column3] => value
        .
        .

        )

    [1] => Array    //row2
        (
            [column1] => value
            [column2] => value
            [column3] => value
        .
        .
        )

    [2] => Array    //row3
        (
            [column1] => value
            [column2] => value
            [column3] => value
        .
        .
        )

)

我是新手,请帮忙.

推荐答案

    $results = array();
    while($row = mysql_fetch_assoc($result))
    {
        $results[] = $row;
    }

    //$results has all that you need
    print_r($results);

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

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