$data = [ ['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Jane'], ['id' => 3, 'name' => 'Mark'], ]; $indexedData = \yii\helpers\ArrayHelper::index($data, 'id');
[ 1 => ['id' => 1, 'name' => 'John'], 2 => ['id' => 2, 'name' => 'Jane'], 3 => ['id' => 3, 'name' => 'Mark'], ]
$data = [ ['id' => 1, 'name' => 'John', 'age' => 25], ['id' => 2, 'name' => 'Jane', 'age' => 32], ['id' => 3, 'name' => 'Mark', 'age' => 28], ]; $indexedData = \yii\helpers\ArrayHelper::index($data, function ($item) { return $item['age']; });
[ 25 => ['id' => 1, 'name' => 'John', 'age' => 25], 32 => ['id' => 2, 'name' => 'Jane', 'age' => 32], 28 => ['id' => 3, 'name' => 'Mark', 'age' => 28], ]It is a part of the Yii framework package library.