PHP Riddle: Array Loop Creation

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Solve me this riddle I seem to have somehow created :lol: I can't solve it (at least yet) and yet it's probably something elementary...

The PHP Code:
PHP Code: [ Select ]
// 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;
  1. // Getting the user's permissions
  2. $sql = "SELECT permission_name, permission_value
  3.        FROM " . PERM_TABLE . "
  4.        WHERE permission_user_id = " . $this->user_id();
  5.  
  6. // Running the SQL
  7. $result = $this->db->query($sql);
  8.  
  9. // Fetching user's permissions
  10. $user_perms = $this->db->fetch_arow($result, 'num');
  11.  
  12. // Initiating the permission array
  13. $permission = array();
  14.  
  15. // Looping through the permissions
  16. foreach($user_perms as $value)
  17. {
  18.     print_r($value);
  19.     $permission[$value[0]] = $permission[$value[1]];
  20. }
  21.  
  22. print_r($permission);exit;

...and the output...
Code: [ Select ]
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] =>
)
  1. Array
  2. (
  3.   [0] => Post
  4.   [1] => 1
  5. )
  6. Notice: Undefined offset: 1 in C:\xampp\htdocs\www\cms\includes\user.php on line 133
  7. Array
  8. (
  9.   [0] => Message
  10.   [1] => 1
  11. )
  12. Notice: Undefined offset: 1 in C:\xampp\htdocs\www\cms\includes\user.php on line 133
  13. Array
  14. (
  15.   [Post] =>
  16.   [Message] =>
  17. )

Those offset 1 that is undefined seems defined to me... it's there... unless it's a ghost... ...

For the time being, I can't seem to solve it...
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

PHP Code: [ Select ]
$user_perms = $this->db->fetch_arow($result, 'num');

The $this->db->fetch_arow(...); function does the following:
PHP Code: [ Select ]
// We do a numeric array
while($row = $result->fetch_array(MYSQLI_NUM))
{
    $rows[] = $row;
}
 
// Returning the array
return $rows;
  1. // We do a numeric array
  2. while($row = $result->fetch_array(MYSQLI_NUM))
  3. {
  4.     $rows[] = $row;
  5. }
  6.  
  7. // Returning the array
  8. return $rows;

Which gives me the following array:
Code: [ Select ]
  Array
  (
   [0] => Array
    (
     [0] => Post
     [1] => 1
    )
   [1] => Array
    (
     [0] => Message
     [1] => 1
    )
  )
  1.   Array
  2.   (
  3.    [0] => Array
  4.     (
  5.      [0] => Post
  6.      [1] => 1
  7.     )
  8.    [1] => Array
  9.     (
  10.      [0] => Message
  11.      [1] => 1
  12.     )
  13.   )
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Ok, I managed to solve my riddle :lol: and it was something really easy to begin with.

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

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

I am not sure why I was doing it that way from the beginning... must've had a brain fart or something :lol:

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 108 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.