2017-09-21 54 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; 
    }