Skip to content
Advertisement

Using php class for storing and grouping data to each person in array using different IDs

I have SQL exercise where I should convert table data into a print sentence where every person has phone numbers. For example

JavaScript

But instead Alice: n1 and Alice: n2 it should be Alice: n1, n2. Alice: n5 has a different ID and we aren’t limited to a unique name. So we should get :

JavaScript

I’m pretty sure I missed something since I’m getting each person phone(s) separately in $personList

JavaScript

addPhone() is just a function in Person class file

JavaScript

Maybe I should use up this $dictionary = [] array, to store values, but I’m still confused how should I do it then. Cannot use database to handle the concatenation.

Advertisement

Answer

A more efficient way to do this would be to index your array by ID. Your code becomes:

JavaScript

Note: you can also return array_values($personList) if you don’t want the array indexed by ID when retrieving it.

Demo

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement