Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将MySQLI fetch_assoc()压缩为每列一个数组_Php_Mysql_Json_Mysqli - Fatal编程技术网

Php 将MySQLI fetch_assoc()压缩为每列一个数组

Php 将MySQLI fetch_assoc()压缩为每列一个数组,php,mysql,json,mysqli,Php,Mysql,Json,Mysqli,感谢和,我能够使用MySQLi和方法链接在MySQL查询中填充json对象 /// Populate an array with the results; public function populate(){ ($rows=array()); while($row=$this->result->fetch_assoc()) $rows[]=$row; $this->rows=$rows; $this->last=

感谢和,我能够使用MySQLi和方法链接在MySQL查询中填充json对象

  /// Populate an array with the results;
  public function populate(){
    ($rows=array());

    while($row=$this->result->fetch_assoc())
      $rows[]=$row;

    $this->rows=$rows;

    $this->last=$this->rows;
    return $this;
  }
我得到

[
  {
      "id": 1,
      "datein": "2012-06-06 09:59:05"
  },
  {
      "id": 2,
      "datein": "2012-06-06 11:32:45"
  },
  {
      "id": 3,
      "datein": "2012-06-07 00:47:19"
  }
]
我怎样才能取而代之

{
  "id": [1,2,3]
  "datein": ["2012-06-06 09:59:05","2012-06-06 11:32:45","2012-06-07 00:47:19"]
}
为了有一个替代和紧凑的版本的结果

非常感谢你的帮助

编辑: 多亏了您的帮助,我准备了这种mysql包装,并提供了两种获取方法:

只需在数组中初始化两个数组,一个用来保存
id
另一个用来保存
datein

public function populate(){
$rows = array('id'=>array(), 'datein'=>array());

while($row=$this->result->fetch_assoc())
  $rows['id'][] = $row['id'];
  $rows['datein'][] = $row['datein'];

$this->rows = $rows;

$this->last = $this->rows;
return $this;

}您可以将代码更改为:

/// Populate an array with the results;
public function populate() {
    $rows = array();

    $this->result = array();
    while ($row = $this->result->fetch_assoc())
        foreach ($row as $field => $value)
            $this->result[$field][] = $value;

    return $this;
}

你会发现你的第一个版本更好,更容易使用。我反对你的意见。谢谢你的建议。事实上,我会有这两种方法,我可以根据情况使用其中一种或另一种。无论如何,对我提出另一种获取数据的方法进行否决是有点奇怪的:|我想否决是因为你没有严格尝试达到你想要的结果,但这个问题看起来像是一个经过适当伪装的问题“给我密码”第一,我没投你反对票。