<?php
// result from db
$recordset1 = [
[
'id' => 1337,
'name' => 'Foo',
'age' => '40',
'sex' => 'yes please',
], [
'id' => 1347,
'name' => 'Foo',
'age' => '40',
'sex' => 'yes please',
],
];
// this I want
$recordset = [
1337 => [
'name' => 'Foo',
'age' => '40',
'sex' => 'yes please',
],
1347 => [
'name' => 'Foo',
'age' => '40',
'sex' => 'yes please',
],
];
$result = array_map(function($record){ return array_diff_key($record, ['id'=>1]); }, array_column($recordset, null, 'id'));
var_dump($result);
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- array(2) {
[0]=>
array(3) {
["name"]=>
string(3) "Foo"
["age"]=>
string(2) "40"
["sex"]=>
string(10) "yes please"
}
[1]=>
array(3) {
["name"]=>
string(3) "Foo"
["age"]=>
string(2) "40"
["sex"]=>
string(10) "yes please"
}
}
- Output for 5.4.0 - 5.4.45
- Fatal error: Call to undefined function array_column() in /in/MCrX3 on line 33
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/MCrX3 on line 4
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected '[' in /in/MCrX3 on line 4
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/MCrX3 on line 4
Process exited with code 255.
preferences:
267.68 ms | 401 KiB | 354 Q