php 获取具有键名称的单个数组中的所有数组值

标签 php mysql

我编写这些函数是为了在 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;
    }

最佳答案

尝试以下操作,它应该对您有用。

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

关于php 获取具有键名称的单个数组中的所有数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337994/

相关文章:

php - AngularJS 无法将数据插入 phpMyadmin (mySql)

php - javascript 框不显示 - 值

php - <> 和 != 有什么区别

php - 使用 PHP、PDO、MYSQL 将 Android 推送通知发送到多个设备

mysql - 次要关系的唯一性约束

mysql - 计算mysql中COUNT()的平均值

php preg_match 只匹配数字、字母和点

mysql - 当我直接从 MySQL 获取时,NodeJS 响应 MySQL 时区不同

MySQL:将具有相同列数据的所有行导出到同一个文件中

php - 迁移后无法访问 wordpress 登录页面