在php中组合数组的两个值

时间:2022-09-25 12:40:24

I have selected two colum of the database as below

我已经选择了两个数据库列,如下所示

$sort_query = "SELECT s_uniqid, mar_total FROM record WHERE $x='$y'";
    $run_sort = mysql_query($sort_query);
    while($sort_marks = mysql_fetch_assoc($run_sort))
    {
        foreach($sort_marks as $key => $marks)
        {
            print_r ($marks);
            echo "<br/>";
        }
    }

I am getting the result as

我得到的结果是

1000001
252
1000002
257
1000003
232
1000004
180
1000005
205
1000006
189
1000007
219
1000008
201

I want to make a new array with the element where key as 1000001, 1000002, 1000003 , 1000004 and value is 252,257,232,180 and so on. Please suggest anything.

我想用一个元素创建一个新数组,其中key为1000001,1000002,1000003,1000004,值为252,257,232,180,依此类推。请提出建议。

1 个解决方案

#1


0  

$arr = array();
while($sort_marks = mysql_fetch_assoc($run_sort))
{
    $arr[$sort_marks['s_uniqid']] = $sort_marks['mar_total'];
}

If you have read the documentation for mysql_fetch_assoc(), this should be self-explanatory. Column s_uniqid is used for the keys, and column mar_total is used for the values.

如果您已经阅读了mysql_fetch_assoc()的文档,那么这应该是不言自明的。列s_uniqid用于键,列mar_total用于值。

#1


0  

$arr = array();
while($sort_marks = mysql_fetch_assoc($run_sort))
{
    $arr[$sort_marks['s_uniqid']] = $sort_marks['mar_total'];
}

If you have read the documentation for mysql_fetch_assoc(), this should be self-explanatory. Column s_uniqid is used for the keys, and column mar_total is used for the values.

如果您已经阅读了mysql_fetch_assoc()的文档,那么这应该是不言自明的。列s_uniqid用于键,列mar_total用于值。