Convert the Yii query result to an array

Source: Internet
Author: User

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 ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.