I have a MySQL table types
where I store product types. I fetch them all and get this array:
[0]=>
['unique_codename']=>'cars'
['category']=>'vehicle'
…some other stuf…
[1]=>
['unique_codename']=>'fruit'
['category']=>'food'
…some other stuf…
[2]=>
['unique_codename']=>'vegetables'
['category']=>'food'
…some other stuf…
…
Next, I have a table which contains specific products
. I could fetch them all and get:
[0]=>
['codename']=>'fruit'
['name']=>'banana'
…some other stuf…
[1]=>
['codename']=>'fruit'
['name']=>'apple'
…some other stuf…
[2]=>
['codename']=>'vegetables'
['name']=>'cauliflower'
…some other stuf…
[3]=>
['codename']=>'cars'
['name']=>'audi'
…some other stuf…
[4]=>
['codename']=>'cars'
['name']=>'volvo'
…some other stuf…
…
I want to construct one single array that contains all the info, like so:
[0]=>
['unique_codename']=>'cars'
['sorts']=>
[0]=>
['name'] = 'audi'
…
[1]=>
['name'] = 'volvo'
…
['category']=>'vehicle'
…
[1]=>
['unique_codename']=>'fruit'
['sorts']=>
[0]=>
['name'] = 'banana'
…
[1]=>
['name'] = 'apple'
…
['category']=>'food'
…
[2]=>
['unique_codename']=>'vegetables'
['sorts']=>
[0]=>
['name'] = 'cauliflower'
…
['category']=>'food'
…
…
I was thinking about first fetching both arrays. Next, I could push the second array in the first in the appropriate place, but I can't figure out how that would work when using array_push
. Anyone who can help me? Or is there a more elegant solution to accomplish this?