感谢检测数据类型,同时使用fetch_array与MySQLi和输出属性与PHP5和方法链,我能够使用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包装和两个抓取方法提供:http://ross.iasfbo.inaf.it/gloria/decibel-class
在数组中初始化2个数组,一个用于保存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;
}