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:
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 |
$post=Post::model()->findByAttributes($attributes,$condition,$params);
That's it.
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
|
|
|
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