Yii2 Array Helper Class Arrayhelper

Source: Internet
Author: User

Tag: Gets the value class name a static method. com www map ddd

Array Helper Class Arrayhelper1. What is the array helper class

The Yii Array helper class provides additional static methods that allow you to work with arrays more efficiently.

A, Get value (GetValue)

class user{  public$name = ' Alex ';}
$array = [ ' foo ' = = [ new User (), DDD ]]

Get the value of name

PHP Method:

$value isset ($array$arraynull;

arrayhelper Method:

$value = Arrayhelper::getvalue ($array, ' foo.bar.name ');
2. Get column (GetColumn)

Gets the value of a column from a multidimensional array or an array of objects

$data = [[///  key equals the property name of the object, value equals the object's property values [' id ' = ' 456 ', ' data ' = ' def '],
    
     ]
    

Arrayhelper Method:

$ids = Arrayhelper::getcolumn ($data, ' id ');

Results: [' 123 ', ' 456 '];

3. Build a mapping table (map)

You can use the map method to extract data from a multidimensional array or an array of objects, create a mapping table (a key-value array),
Specifies the key name and value of the pre-built mapping table in the parameter list of the map method.

$array = [' id ' = ' 123 ', ' name ' = ' aaa ', ' email ' = ' x '],[' id ' = ' 456 ', ' name ' = > ' BBB ', ' email ' = ' y ',[' id ' = ' = ' 789 ', ' name ' = ' CCC ', ' email ' = ' ' Z '], '// Arrayhelper Method: $result = Arrayhelper::map ($array, ' id ', ' name '); /* result: [' 123 ' = ' aaa ' ' 456 ' + ' bbb ' 789 ' + ' CCC,] */

Note: This article for the author (44106-kangaroo) after reading Wei teach you to learn Yii2.0 video after the note, if reproduced please indicate the source: http://www.cnblogs.com/chrdai/p/8006752.html

Yii2 Array Helper Class Arrayhelper

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.