从数据库结果PHP多维数组 [英] PHP multidimensional array from database results

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

问题描述

我是一个有点新的多维数组,并想看看如果我这样做的权利。 preferably,我想命名主阵列内的阵列的易用性。

  $ unique_array =阵列(
    用户名=>阵()
    USER_ID =>阵()
    weeknumber =>阵()
    );

然后我有一个while循环,它检查一些数据库的结果:

 而($行= mysql_fetch_array($查询))//是的,我知道是mysql德precated
{
$ unique_array [] =用户名=> $行[用户名],USER_ID => $行['USER_ID'],周数=>'weeknumber'];
}

我不知道如果我从while循环中正确地安装该数组中,或者是否需要做一些其他的方式。我找不到任何资源,我可以很容易地在SO或其他地方多维数组内的指定数组内处理查询结果明白了。

编辑跟进问:我还需要检查的数组中重复的值,因为那里将是完全相同的多个值,但我只希望他们中的一个。

任何帮助AP preciated!

编辑答案:

通过修改答案,我能创造code适合我的需要。

数组初始化:

  $ unique_array =阵列(
    用户名= GT;阵()
    'USER_ID =>阵()
    weeknumber'=>阵()
    );

从while循环中构建数组:

 而($行= mysql_fetch_array($查询))
{
$ unique_array [] =阵列('用户名'=> $行[用户名],'USER_ID => $行['USER_ID'],'weeknumber'=> $行['weeknumber']);
}

最后,我需要确保数组值是唯一的(有重复条目的数据库和查询限制的结果),while循环后,我有:

 的print_r(multi_unique($ unique_array));


解决方案

是顶级关联数组或数字阵列?

如果它是一个关联数组,它应该具有的结构是这样的:

  $ unique_array =阵列(
    用户名= GT;阵列('约翰','迈克',...)
    'USER_ID'=>阵列(1,2,3,...),
    WEEK_NUMBER'=>阵列(1,2,3,...)
);

或者,如果它是一个数值数组,它应该具有的结构是这样的:

  $ unique_array =阵列(
    阵列('用户名'= GT;'约翰','USER_ID =大于1,'WEEK_NUMBER'=> 1)
    阵列('用户名'= GT;'迈克','USER_ID =大于2,'WEEK_NUMBER'=> 2),
    阵列('用户名'= GT;'山姆','USER_ID =→3'WEEK_NUMBER'=> 3)
    ...

对于第一种使用code如下:

 而($行= mysql_fetch_assoc($查询)){
    $ unique_array [用户名] [] = $行[用户名]
    $ unique_array ['USER_ID'] [] = $行['USER_ID'],
    $ unique_array ['WEEK_NUMBER'] [] = $行['WEEK_NUMBER'],
}

对于第二类,它是像你code。但也有一些语法问题:

 而($行= mysql_fetch_array($查询))//是的,我知道是mysql德precated
{
    $ unique_array [] =阵列('用户名'=> $行[用户名],'USER_ID => $行['USER_ID'],'WEEK_NUMBER'=> $行['weeknumber']);
}

I'm a bit new to multidimensional arrays, and would like to see if I'm doing it right. preferably, I'd like to name the arrays within the main array for ease of use.

$unique_array = array(
    username=>array(),
    user_id=>array(),
    weeknumber=>array()
    );

and then I have a while loop which checks some database results:

while($row = mysql_fetch_array($query)) //yes, I know mysql is deprecated
{
$unique_array[] = username=>$row['username'], user_id=>$row['user_id'], week number=>['weeknumber'];
}

I'm not sure if I am placing the values in the array from within the while loop correctly, or if it needs to be done some other way. I couldn't find any resources I could easily understand on SO or elsewhere to deal with query results within a named array within a multidimensional array.

EDIT FOLLOW UP QUESTION: I also need to check the array for duplicate values, because there will be multiple values that are exactly the same, but I only want one of them.

Any help is appreciated!

EDIT SOLUTION:

By modifying the answer I was able to create code to fit my needs.

Array initialization:

$unique_array = array(
    'username'=>array(),
    'user_id'=>array(),
    'weeknumber'=>array()
    );

Building the array from within a while loop:

while($row = mysql_fetch_array($query))
{
$unique_array[] = array('username'=>$row['username'], 'user_id'=>$row['user_id'], 'weeknumber'=>$row['weeknumber']);
}

And finally, I need to make sure the array values are unique (there are duplicates entries as a result of database and query limitations), after the while loop I have:

print_r(multi_unique($unique_array));

解决方案

Is the top level an associative array or a numeric array?

If it is an associative array, it should have structure like this:

$unique_array = array(
    'username'=>array('John','Mike',...),
    'user_id'=>array(1,2,3,...),
    'week_number'=>array(1,2,3,...)
);

Or if it is a numeric array, it should have structure like this:

$unique_array = array(
    array('username'=>'John', 'user_id'=>1, 'week_number'=>1),
    array('username'=>'Mike', 'user_id'=>2, 'week_number'=>2),
    array('username'=>'Sam', 'user_id'=>3, 'week_number'=>3),
    ...
)

for the first type use the code below:

while ($row = mysql_fetch_assoc($query)) {
    $unique_array['username'][] = $row['username'],
    $unique_array['user_id'][] = $row['user_id'],
    $unique_array['week_number'][] = $row['week_number'],
}

for the second type, it is something like your code. But there are some syntax problems:

while($row = mysql_fetch_array($query)) //yes, I know mysql is deprecated
{
    $unique_array[] = array('username'=>$row['username'], 'user_id'=>$row['user_id'], 'week_number'=>$row['weeknumber']);
}

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

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