我想创建一个带有ID的新数组,用户可以访问扇区。
$sector = '60';
在这种情况下使用用户ID(2和4)创建新数组...
$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
)
)
答案 0 :(得分:1)
使用user_id
作为数组的键,并使用[]
附加user_sectors
数组项:
while ($row = mysql_fetch_array($result)) {
$sectors[$row['user_id']][] = unserialize(base64_decode($row['user_sectors']));
}
话虽如此,对于mysql_*()
函数:
警告此扩展在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。相反,MySQLi或PDO_MySQL扩展 应该使用。另请参阅MySQL:选择API指南和相关的常见问题解答 欲获得更多信息。该功能的替代方案包括:
- mysqli_fetch_array()
- PDOStatement对象::取()