2017-09-21 82 views
0

我寫這些函數用於在php.But數據庫中獲取記錄,但在這個數組中,我得到了單個數組中的所有值。我想要得到 這種類型的result.What做什麼來獲得這種類型的數組。php獲取單個數組中的所有數組值與密鑰名稱

Example:  
Array 
(
    [city] => Array 
     (
      [0] => ABINGTON 
      [1] => CHELTENHAM 
      [2] => PHILADELPHIA 
     ) 
    [state] => Array 
     (
      [0] => Florida 
      [1] => Other 

     ) 

public function selectLocations($POST) 
    { 
     $f1=0; 
     $location = array(); 
     $SELECT_CITY = "SELECT DISTINCT city FROM listings WHERE Matrix_Unique_ID > 0 LIMIT 0,5"; 
     $cityresult = $this->conn->query($SELECT_CITY); 
     while($row = $cityresult->fetch_assoc()) 
     { 
      $location[$f1] = $row['city']; 
      $f1++; 
     } 

     $SELECT_STATE = "SELECT DISTINCT state_or_province FROM listings WHERE Matrix_Unique_ID > 0"; 
     $stateresult = $this->conn->query($SELECT_STATE); 

     while($row1 = $stateresult->fetch_assoc()) 
     { 
      $location[$f1] = $row1['state_or_province']; 
      $f1++; 
     } 

     return $location; 
    } 
+0

任何解決方案? –

回答

0

嘗試以下,它應該爲你工作。

public function selectLocations($POST) 
    { 
     $location = array(); 
     $SELECT_CITY = "SELECT DISTINCT city FROM listings WHERE Matrix_Unique_ID > 0 LIMIT 0,5"; 
     $cityresult = $this->conn->query($SELECT_CITY); 
     while($row = $cityresult->fetch_assoc()) 
     { 
      $location['city'][] = $row['city']; 
     } 

     $SELECT_STATE = "SELECT DISTINCT state_or_province FROM listings WHERE Matrix_Unique_ID > 0"; 
     $stateresult = $this->conn->query($SELECT_STATE); 

     while($row1 = $stateresult->fetch_assoc()) 
     { 
      $location['state'][] = $row1['state_or_province']; 
     } 

     return $location; 
    }