The array helper class for Yii

Source: Internet
Author: User
Tags yii

Get value

It is cumbersome to use native PHP to get data from an object, an array, or a complex data structure that contains both. You first have to isset check if the key exists and then if it exists you get it, and if it does not, provide a default return value:

YII provides a very convenient way to do this:

The first parameter of a method is where we get the value. The second parameter specifies how to get the data, which can be one of the following types:

    • The name of the array key or the property of the object from which to take the value;
    • A string that consists of the array key name or the object property name of the dot partition, which is the type of the parameter used in the previous example;
    • Returns a value for the callback function.

The callback function is shown in the following example:

The third optional parameter, if no value is given, is the default null , as shown in the following example:

For situations where you want to remove a value immediately from the array, you can use the remove method:

After executing the above code, it will be $array included and will be [‘options‘ => [1, 2]] $type A . Note getValue Unlike the method, the remove method only supports simple key names.

Check the existence of the key name

ArrayHelper::keyExistsWorks the same as array_key_exists, except that it can support case-insensitive key-name comparisons, such as:

Retrieving columns

Usually you get the value of a column from multiple rows of data or arrays of multiple objects, and a common example is getting a list of ID values.

The result will be [‘123‘, ‘345‘] .

If additional conversions or values are required, the second parameter can specify an anonymous function:

Rebuilding an array index

A method can be used to re-index an array by a specified key name index . The input array should be a multidimensional array or an array of objects. Key Name (Translator Note: The second argument) can be the key name of the sub-array, the object's property name, or an anonymous function that returns the key value of the given element array.

The result would be a associative array, where the key is the value of id attribute

Anonymous function, passed as a $key , gives the same result.

Passing as id a third argument would group $array by id :

The result is a multidimensional array grouped by on the first level and not indexed on the id second level:

An anonymous function can is used in the grouping array as well:

The result is a multidimensional array grouped by on id the first level, by the device second level and indexed By on the data third level:

Set up a hash table

To create a mapping table (a key-value pair) from a multidimensional array or an array of objects, you can use the map method. The $from key and $to property names of the mapping table to be constructed are specified separately from the parameters. Depending on your needs, you can group the mapping table by a grouping field $group , for example,

Multidimensional sorting

multisortMethod can be used to sort nested arrays or arrays of objects, sorted by one or more key names, for example,

The values we get in the following order are $data as follows:

The second parameter specifies the key name of the sort, if it is a single-key name, it can be a string, if it is a multi-keys name is an array, or is an anonymous function as shown in the following example:

The third parameter indicates the order of increment. When a single bond is sorted, it can be SORT_ASC either one or the other SORT_DESC . If you sort by multiple key names, you can use an array to specify a different order for each key.

The last parameter (translator Note: The fourth parameter) is the sort flag of PHP, the value that can be used and the value passed when the PHP sort () function is called.

Detecting array types

It's convenient to know whether an array is an indexed array or a union array, and here's an example:

HTML encoding and decoding values

To make HTML codecs for special characters in a string array, you can use the following methods:

The default is to encode only the values (the translator notes: The source code is encoded, it should be codec). falseYou can also encode a key name by passing it to the second argument. The encoding will use the application's character set by default, and you can specify the character set by the third parameter.

Merging arrays

Object conversions to arrays

You often want to convert an array of objects or objects into an array, and the common scenario is to convert an AR model (the activity record model) into an arrays in order to provide an array of data (or other usage) from the rest API. The following code can do the work:

The first parameter contains the data that we want to convert, in this case we are going to convert a called Post AR model.

The second parameter is the conversion mapping table for each class, where we set up a Post mapping of the model. Each map array contains a set of mappings, each of which can be:

    • A field name to be included as is (and the name of the attribute in the class is the same);
    • A key-value pair consisting of a key name that you can randomly name and the name of the model column from which you want to take a value;
    • A key-value pair consisting of a key name that you can freely name and a callback function with a return value;

The result of this conversion will be:

You can also implement the Arrayable interface in a particular class to provide its objects with a default method for converting an array.

Testing against Arrays

Often you need to check if an element was in an array or a set of elements is a subset of another. While PHP offers in_array() , this does is not a support subsets or \Traversable objects.

To aid these kinds of tests, Yii\base\arrayhelper provides yii\base\arrayhelper::isin () and yii\base\ Arrayhelper::issubset () with the same signature as In_array ().

The array helper class for Yii

Related Article

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.