PHP谜语:数组循环创建 [英] PHP Riddle: Array Loop Creation
本文介绍了PHP谜语:数组循环创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从另一个关联数组创建一个数组,并且脚本找不到代码。
这是我的PHP代码使用:
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:
// 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] =>
)
那些未定义的偏移量1似乎定义给我......它就在那里......除非它是幽灵......
Those offset 1 that is undefined seems defined to me... it's there... unless it's a ghost... ...
$user_perms = $this->db->fetch_arow($result, 'num');
此行确实...
This line does...
// 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
)
推荐答案
sql = SELECT permission_name,permission_value
FROM。 PERM_TABLE。
WHERE permission_user_id =。
sql = "SELECT permission_name, permission_value FROM " . PERM_TABLE . " WHERE permission_user_id = " .
this - > USER_ID();
// 运行SQL
this->user_id(); // Running the SQL
result =
result =
这篇关于PHP谜语:数组循环创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文