php使用键名在单个数组中获取所有数组值

时间:2017-09-21 07:32:45

标签: php mysql

我为php中的数据库中的fetch记录编写了这些函数。但是在这个数组中,我得到了一个数组中的所有值。我想得到     这种类型的结果。如何获得这种类型的数组。

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;
    }

1 个答案:

答案 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;
    }