-2

I have an array of Objects. These Objects have different keys with arrays as value. As an output I need a combined array of all these Array. Example:

 [
  {
    id: 1,
    datas: [a,b,c]
  }, 
  {
    id: 2,
    datas: [d,e,f]
  },
  {
    id: 3,
    datas: [f,g,h]
  }
]

As output I need: [a,b,c,d,e,f,g,h] I couldnt find an easy one liner so far. I could iterate over the Array and merging all arrays in a new one. But yeah, I like one liner and as far as I know PHP there is a quick way for that

Introser
  • 161
  • 1
  • 12
  • are you asking for a pythonic one liner in PHP? – bartholomew Mar 04 '21 at 15:04
  • Under the hood, any one liner would do the exact same thing your algorithm does - iterate and merge all these arrays together. Any specific reason why you insist on a one liner? You say "quick way", but I don't know what that represents for you - code that executes faster or just something that requires less typing? – El_Vanja Mar 04 '21 at 15:11

2 Answers2

3

Your oneliner is:

print_r(array_merge(...array_column($array, 'datas')));

Working fiddle.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
-1

Use this function array_push();

Samad
  • 34
  • 1
  • 6