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
)
)
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()