使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回。比如下面的方法:
<?php // 查找满足指定条件的结果中的第一行 $post=Post::model()->find($condition,$params); // 查找具有指定主键值的那一行 $post=Post::model()->findByPk($postID,$condition,$params); // 查找具有指定属性值的行 $post=Post::model()->findByAttributes($attributes,$condition,$params);
<?php Post::model()->find()->attributes
<?php
//第一种直接将结果循环输出
foreach ($myReceivedCode as $model) {
$result[] = $model->attributes;
}
//第二种用array_map
$result= array_map(function($record) {
return $record->attributes;
}, Post::model()->findAllByAttributes($attributes));版权声明:本文为博主原创文章,未经博主允许不得转载。
Yii1.1 Active Record 查询结果转化成数组
原文:http://blog.csdn.net/phpfenghuo/article/details/46843053