Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to create an array from another associative array, and the script isn't finding the code.

This is the PHP code that I'm using:
PHP
// Getting the user's permissions
$sql = "SELECT permission_name, permission_value
       FROM " . PERM_TABLE . "
       WHERE permission_user_id = " . $this->user_id();

// Running the SQL
$result = $this->db->query($sql);

// Fetching user's permissions
$user_perms = $this->db->fetch_arow($result, 'num');

// Initiating the permission array
$permission = array();

// Looping through the permissions
foreach($user_perms as $value)
{
    print_r($value);
    $permission[$value[0]] = $permission[$value[1]];
}

print_r($permission);exit;

And below is what I'm getting out of it.
Array
(
  [0] => Post
  [1] => 1
)
Notice: Undefined offset: 1 in C:\xampp\htdocs\www\cms\includes\user.php on line 133
Array
(
  [0] => Message
  [1] => 1
)
Notice: Undefined offset: 1 in C:\xampp\htdocs\www\cms\includes\user.php on line 133
Array
(
  [Post] =>
  [Message] =>
)

Those offset 1 that is undefined seems defined to me... it's there... unless it's a ghost... ...
PHP
$user_perms = $this->db->fetch_arow($result, 'num');

This line does...
PHP
// We do a numeric array
while($row = $result->fetch_array(MYSQLI_NUM))
{
    $rows[] = $row;
}

// Returning the array
return $rows;

Which gives me an array, which looks like...
Array
(
 [0] => Array
  (
   [0] => Post
   [1] => 1
  )
 [1] => Array
  (
   [0] => Message
   [1] => 1
  )
)

And I'm trying to make that array look like...
Array
(
 [Post] => 1
 [Message] => 1
)
Posted

1 solution

Nevermind, I solved it.

Instead of:
PHP
$permission[$value[0]] = $permission[$value[1]];

I should've being doing:
PHP
$permission[$value[0]] = $value[1];

I'm not exactly sure why I was doing it that way from the beginning... must've had a brain fart or something
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900