D
D
ddddd tttt2018-03-11 12:16:16
PHP
ddddd tttt, 2018-03-11 12:16:16

How to make the first value in a row a key in a two-dimensional array?

There is such an array:
$rows2 = [
['id'=>'a1','update_at' => dddd,'create_at' => cccc],
['id'=>'a2','update_at' => dddd, 'create_at' => cccc]
];
How to make it like this:
$rows2 = [
['a1' => ['update_at' => dddd,'create_at' => cccc]],
['a2' => ['update_at' => dddd,'create_at' = > cccc]],
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2018-03-11
@pashaa

If you leave the id in the lines, then you can do this:
But if you really need to remove:

$result = array_combine(array_column($rows2, 'id'), $rows2);
foreach (array_keys($result) as $id) {
    unset($result[$id]['id']);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question