Reputation: 83
I get back the data from DB with php, and I get it back as follows so I want to modify it.
This what I tried:
$result = $db->run("SELECT * FROM.....")->fetchAll();
foreach($result as $key => $val) {
$result[$key] = $result[$key]['id'];
}
I want to change the key ( 0, 1) to the id values (5, 6) Change this:
{
"0": {
"id": 5,
"date_created": "2021-08-18 03:35:31",
"status": 1
},
"1": {
"id": 6,
"date_created": "2021-08-18 03:35:55",
"status": 1
}
}
To this:
{
"5": {
"id": 5,
"date_created": "2021-08-18 03:35:31",
"status": 1
},
"6": {
"id": 6,
"date_created": "2021-08-18 03:35:55",
"status": 1
}
}
Upvotes: 0
Views: 86
Reputation: 367
try this
$newResult = [];
foreach($result as $key => $val) {
$newResult[$val['id']] = $val;
}
print_r($newResult);
Upvotes: 3