2013-02-11 75 views
0

轉化的MySQL結果陣列

$query = "SELECT 
PrivilageName, 
PrivilageExp, 
'true' hakkivarmi 
FROM users 
natural join usermemberships 
natural join groupprivileges 
natural join `privileges` 
where UserID = '2' "; 

$result = mysqli_query($link, $query); 

$rows = Array(); 
while($row = mysqli_fetch_assoc($result)) { 
    array_push($rows, $row); 
} 

print_r($rows);exit; 

而且它打印出

Array 
(
    [0] => Array 
     (
      [PrivilageName] => AddBuilding 
      [PrivilageExp] => Bina Ekleme 
      [hakkivarmi] => true 
     ) 

    [1] => Array 
     (
      [PrivilageName] => RemoveBuilding 
      [PrivilageExp] => Bina Silme 
      [hakkivarmi] => true 
     ) 

    [2] => Array 
     (
      [PrivilageName] => EditBuilding 
      [PrivilageExp] => Bina Düzenleme 
      [hakkivarmi] => true 
     ) 

) 

而且我要的是

Array 
(
AddBuilding => true 
RemoveBuilding => true 
EditBuilding => true 
) 

我已經嘗試了許多不同的事情,但沒有成功。我該怎麼做?

回答

1
$permissions = array(); 

while($row = mysqli_fetch_assoc($result)) { 
    $permissions[$row['PrivilageName']] = true; // You can use $row['hakkivarmi'] but it looks like it's always true based on your code 
} 

var_dump($permissions); 
1
foreach($rows as $key => $value){ 
    $f[$value['PrivilageName']] = $value['hakkivarmi'] 
} 

var_dump($f);