PHP Class infoweb\cms\helpers\ArrayHelper

Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\helpers\BaseArrayHelper
ファイルを表示 Open project: infoweb-internet-solutions/yii2-cms Class Usage Examples

Public Methods

Method Description
indexRecursive ( array $array, string | Closure $key ) : array Indexes an array according to a specified key.

Method Details

indexRecursive() public static method

The input array should be multidimensional or an array of objects. The key can be a key name of the sub-array, a property name of object, or an anonymous function which returns the key value given an array element. If a key value is null, the corresponding array element will be discarded and not put in the result. For example, ~~~ $array = [ ['id' => '123', 'data' => 'abc'], ['id' => '345', 'data' => 'def'], ]; $result = ArrayHelper::index($array, 'id'); the result is: [ '123' => ['id' => '123', 'data' => 'abc'], '345' => ['id' => '345', 'data' => 'def'], ] using anonymous function $result = ArrayHelper::index($array, function ($element) { return $element['id']; }); ~~~
public static indexRecursive ( array $array, string | Closure $key ) : array
$array array the array that needs to be indexed
$key string | Closure the column name or anonymous function whose result will be used to index the array
return array the indexed array