How do I make an array from specific keys in another array?

OK, I'm exhausted after spending 10 hours researching and testing, but to no avail. I have an array, which is along the lines of ...
[
- {
- 0: {
firstname: "John",
lastname: "Johnson",
age: "34",
etc: "",
etc: "",
etc: ""
},
- 1: {
firstname: "David",
lastname: "Davidson",
age: "45",
etc: "",
etc: "",
etc: ""
},
- 2: {
firstname: "Peter",
lastname: "Peterson",
age: "56",
etc: "",
etc: "",
etc: ""

},
- 3: {
firstname: "Robert",
lastname: "Robertson",
age: "67",
etc: "",
etc: "",
etc: ""
}
}
]
[
- {
- 0: {
firstname: "John",
lastname: "Johnson",
age: "34",
etc: "",
etc: "",
etc: ""
},
- 1: {
firstname: "David",
lastname: "Davidson",
age: "45",
etc: "",
etc: "",
etc: ""
},
- 2: {
firstname: "Peter",
lastname: "Peterson",
age: "56",
etc: "",
etc: "",
etc: ""

},
- 3: {
firstname: "Robert",
lastname: "Robertson",
age: "67",
etc: "",
etc: "",
etc: ""
}
}
]
How do I create a new array to contain only firstnames and lastnames please?
19 Replies
ἔρως
ἔρως5mo ago
select firstname, lastname from table
Blackwolf
BlackwolfOP5mo ago
do you know what, i just realised that as i read it back i've been playing with the complete array
ἔρως
ἔρως5mo ago
what do you mean?
Blackwolf
BlackwolfOP5mo ago
i read the full table into an array, and tried to change THAT array
ἔρως
ἔρως5mo ago
yup, therefore my answer you just need to read the data you need, and you get what you want it's also a lot more performant
Blackwolf
BlackwolfOP5mo ago
i needed all the data initially though to analyse something first. I just need to revisit the table
ἔρως
ἔρως5mo ago
do you still need all the data?
Blackwolf
BlackwolfOP5mo ago
not after analysing it, no, which was why i just wanted to either remove the fields i did not need, or make a new array with what i did but easier to re-read the table having said that, it would be useful to know how to make a new array from an existing one another time i might need firstnames and age
ἔρως
ἔρως5mo ago
you have 2 options: for or array_map
Blackwolf
BlackwolfOP5mo ago
my preference would be the one with the least code 😄
ἔρως
ἔρως5mo ago
for:
for($i = 0, $len = count($array); $i > $len; $i++) {
$array[$i] = [
'firstname' => $array[$i]['firstname'],
'lastname' => $array[$i]['lastname'],
];
}
for($i = 0, $len = count($array); $i > $len; $i++) {
$array[$i] = [
'firstname' => $array[$i]['firstname'],
'lastname' => $array[$i]['lastname'],
];
}
array_map:
$array = array_map(fn($item) => [
'firstname' => $item['firstname'],
'lastname' => $item['lastname'],
], $array);
$array = array_map(fn($item) => [
'firstname' => $item['firstname'],
'lastname' => $item['lastname'],
], $array);
Blackwolf
BlackwolfOP5mo ago
wow, that seems very straight forward
ἔρως
ἔρως5mo ago
it's way easier than it feels like
Blackwolf
BlackwolfOP5mo ago
that worked perfectly @ array_map i shall make a note of both though, thank you very much 🙂
ἔρως
ἔρως5mo ago
you're welcome this should save re-reading from the database
Blackwolf
BlackwolfOP5mo ago
indeed ... now i shall take a well earned break and drink a bucket of coffee 😄
ἔρως
ἔρως5mo ago
i would skip on the bucket of coffee, at this time
Blackwolf
BlackwolfOP5mo ago
it's only 21:20 here 7hours till bedtime
ἔρως
ἔρως5mo ago
terrible time to drink coffee
Want results from more Discord servers?
Add your server