如何更改查询的数组方案结果?

时间:2016-12-14 16:25:04

标签: php mysql arrays oop mysqli

class user{

protected $permissions;

public function can( $permission )
    {
        if (!isset($this->permissions)) {
            /*
             fetch permissions for this user from the database, and set the
             User::permissions property, to allow caching.
             */
            $cnx = $this->connexion();
            $stmt = $cnx->prepare('SELECT permissions.permission_name FROM permissions, users_permissions WHERE users_permissions.user_id = ? AND users_permissions.permission_id = permissions.permission_id');
            $stmt->bind_param('s', $_SESSION["user_id"]);
            $stmt->execute();
            $result = $stmt->get_result();

            $people = array("Peter", "Joe", "Glenn", "Cleveland");
            var_dump($people);
            $this->permissions= $result->fetch_all(MYSQLI_ASSOC);    

            var_dump($this->permissions);    
        }

        return in_array($permission, $this->permissions);
    }
}

(方案1)数组的结果如下所示:

array (size=8)
  0 => 
    array (size=1)
      'permission_name' => string 'Peut créer une permission' (length=26)
  1 => 
    array (size=1)
      'permission_name' => string 'Peut modifier une permission' (length=28)
  2 => 
    array (size=1)
      'permission_name' => string 'Peut voir des comptes utilisateurs' (length=34)

我尝试了fetch_array而不是fetch_all,但它只返回一行

(方案2)无论如何都要使该数组看起来像这样,因为如果它确实会这样:

array (size=4)
  0 => string 'Peter' (length=5)
  1 => string 'Joe' (length=3)
  2 => string 'Glenn' (length=5)

实例化用户

$that_guy = new user();
if ( $that_guy->can('Peut créer une permission')) {
  echo"yay";
}else {
  echo"you can't";
}

有没有办法改变方案1的数组并使其看起来像方案2的数组?

3 个答案:

答案 0 :(得分:1)

使用mysqli是不可能的,这是你应该使用PDO的一个很好的理由

$cnx = $this->connexion();
$stmt = $cnx->prepare('SELECT permissions.permission_name FROM permissions, users_permissions WHERE users_permissions.user_id = ? AND users_permissions.permission_id = permissions.permission_id');
$stmt->execute([$_SESSION["user_id"]]);
$this->permissions = $stmt->fetchAll(PDO::FETCH_COLUMN);

就是这样 这个代码不仅缩短了两倍,而且还能为您提供所需的确切结果。

答案 1 :(得分:0)

在PHP方面,您可以使用array_column

$this->permissions = array_column($this->permissions, 'permission_name');

另外,我还会使用array_flip

$this->permissions = array_flip(
    array_column($this->permissions, 'permission_name')
);

这样您就可以使用isset($this->permissions[$permission])代替in_array($permission, $this->permissions);,因为它更快。

答案 2 :(得分:0)

循环遍历数组

public function can( $permission )
        {
            if (!isset($this->permissions)) {

                $cnx = $this->connexion();
                $stmt = $cnx->prepare('SELECT permissions.permission_name FROM permissions, users_permissions WHERE users_permissions.user_id = ? AND users_permissions.permission_id = permissions.permission_id');
                $stmt->bind_param('s', $_SESSION["user_id"]);
                $stmt->execute();

                $result = $stmt->get_result();
                $stmt->bind_result($permission_name);
                $stmt->store_result();


                $this->permissions = array();
                while(($row = $result->fetch_assoc())) {
                    $this->permissions[] = $row['permission_name'];
                }


            }

            return in_array($permission, $this->permissions);
        }

结果看起来就像你想要的那样(方案2)