Index arrays by id

Is there a way to have array indexes be item id ?
I mean, instead of having
[ 0 [
‘id’ => 2345
‘name’ => ‘xxxxx’,
‘data’ => ‘mlkjhgui’
]
1 [
‘id’ => 2379
‘name’ => ‘yyyyy’,
‘data’ => ‘hgzrfvafghntbgrf’
]
]
I would like to have :
[ 2345 [
‘id’ => 2345
‘name’ => ‘xxxxx’,
‘data’ => ‘mlkjhgui’
]
2379 [
‘id’ => 2379
‘name’ => ‘yyyyy’,
‘data’ => ‘hgzrfvafghntbgrf’
]
]

I tried to use Cake\Utility\Hash but I can’t make it work. Probably something I don’t understand.

It’s a simple formation of an associative multi-dimensional array.
You can use the simple as like given below:

$arr = [];
$arr = $arr + array(‘id_value’ => array());
array_push($arr[‘id_value’], $iterative_data_array);
print_r($arr);

You will get the expected results you want.

Thanks

Thank you very much.
I expected some cake method could do this directly.
But you’re a great help.

$results = collection($results)->indexBy('id')->toArray(); If $results is a query object, you should be able to just call indexBy on it directly.

Zuluru ! Always at the top !

Thank You

Thank you saurabhkackar also for taking time to answer.