This article describes how to convert the Yii query result into an array. If you are interested in this article, please refer to it.
When Yii's Active Record is used to obtain the query result, the returned result set is of an object type. Sometimes it is expected to be converted to an array for data processing convenience. For example, the following method:
The Code is as follows: |
Copy code |
// Find the first row in the result that meets the specified condition $ Post = Post: model ()-> find ($ condition, $ params ); // Find the row with the specified primary key value $ Post = Post: model ()-> findByPk ($ postID, $ condition, $ params ); // Search for rows with the specified property value $ Post = Post: model ()-> findByAttributes ($ attributes, $ condition, $ params );
|
You can.
The Code is as follows: |
Copy code |
Post: model ()-> find ()-> attributes |
If multiple results are returned, there are two methods to return an array of objects:
The Code is as follows: |
Copy code |
// The first method outputs the result cyclically. Foreach ($ mydomainedcode as $ model ){ $ Result [] = $ model-> attributes; }
// Array_map $ Result = array_map (function ($ record ){ Return $ record-> attributes; }, Post: model ()-> findAllByAttributes ($ attributes ));
|