dongyou6909 2017-03-11 01:49
浏览 10
已采纳

从MySQL查询创建新数组

I'd like to create new array with IDs where users have access to sector.

$sector = '60';

Create new array with User IDs (2 and 4) in this case...

$result = mysql_query("SELECT user_id, user_sectors FROM admin_users"); 
    while ($row = mysql_fetch_array($result)) { 
        $id[] = $row[user_id]; 
        $sectors[] = unserialize(base64_decode($row[user_sectors])); 
    }   

$combine = array_combine($id, $sectors);
print "<pre>";
print_r($combine);
print "</pre>";

Array
(
[2] => Array <<<<<<<<<< USER ID
    (
        [0] => 49 <<<<<<<<<< SECTORS
        [1] => 60
        [2] => 69
        [3] => 65
        [4] => 66
        [5] => 59
        [6] => 71
        [7] => 57
    )

[3] => Array <<<<<<<<<< USER ID
    (
        [0] => 49 <<<<<<<<<< SECTORS
    )

[4] => Array <<<<<<<<<< USER ID
    (
        [0] => 49 <<<<<<<<<< SECTORS
        [1] => 60
        [2] => 65
        [3] => 58
    )
)
  • 写回答

1条回答 默认 最新

  • dpjpo746884 2017-03-11 02:04
    关注

    Use the user_id as the key of the array and append [] an array item with the user_sectors:

    while ($row = mysql_fetch_array($result)) { 
        $sectors[$row['user_id']][] = unserialize(base64_decode($row['user_sectors']));
    } 
    

    That being said, for mysql_*() functions:

    Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

    • mysqli_fetch_array()
    • PDOStatement::fetch()
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中