Home >Backend Development >PHP Tutorial >Yii query results into array method_PHP tutorial

Yii query results into array method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:101392browse

This article will share with you the method of converting Yii query results into arrays. If you are interested in this article, feel free to read it for reference.


When using Yii's Active Record to obtain query results, the returned result set is of object type. Sometimes, for the convenience of data processing, it is hoped that it can be converted into an array and returned. For example, the following method:

$post=Post::model()->findByAttributes($attributes,$condition,$params);
The code is as follows
 代码如下 复制代码


// 查找满足指定条件的结果中的第一行
$post=Post::model()->find($condition,$params);
// 查找具有指定主键值的那一行
$post=Post::model()->findByPk($postID,$condition,$params);
// 查找具有指定属性值的行
$post=Post::model()->findByAttributes($attributes,$condition,$params);

Copy code


 代码如下 复制代码

Post::model()->find()->attributes

// Find the first row in the results that meets the specified conditions

$post=Post::model()->find($condition,$params);
// Find the row with the specified primary key value

$post=Post::model()->findByPk($postID,$condition,$params);
 代码如下 复制代码
//第一种直接将结果循环输出
 foreach ($myReceivedCode as $model) {
                        $result[] = $model->attributes;
                }
 
//第二种用array_map
                $result= array_map(function($record) {
                                return $record->attributes;
                        }, Post::model()->->findAllByAttributes($attributes));
// Find rows with specified attribute value

The code is as follows Copy code
Post::model()->find()->attributes If multiple results are returned, there are the following two methods when returning an object array:
The code is as follows Copy code
//The first method directly outputs the results in a loop foreach ($myReceivedCode as $model) { $result[] = $model->attributes;                                                                                                     //The second type uses array_map                         $result= array_map(function($record) { Return $record->attributes;                                          }, Post::model()->->findAllByAttributes($attributes)); http://www.bkjia.com/PHPjc/632655.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632655.htmlTechArticleThis article will share with you the method of converting Yii query results into arrays. If you are interested in this article, Please refer to it to prevent entry. When using Yii's Active Record to obtain query results...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn