0

I'm wondering if someone can help with the following please? Currently I have this array:

    array:1 [
      "blocks" => array:3 [
        0 => array:1 [
          "component" => "TextColumns"
        ]
        1 => array:2 [
          0 => array:1 [
            "component" => "TextColumns"
          ]
          1 => array:1 [
            "component" => "TextColumns"
          ]
        ]
        2 => array:1 [
          "component" => "TextColumns"
        ]
      ]
    ]

What I want to achieve is a final array of the below:

    array:1 [
      "blocks" => array:3 [
        0 => array:1 [
          "component" => "TextColumns"
        ]
        1 => array:1 [
          "component" => "TextColumns"
        ]
        2 => array:1 [
          "component" => "TextColumns"
        ]
        3 => array:1 [
          "component" => "TextColumns"
        ]
      ]
    ]

If I use array_flatten or laravels ->flatten() I do not get the desired results as it flattens it too much:

    array:1 [
      "blocks" => array:3 [
        0 => array:1 [
          "component" => "TextColumns"
        ]
        1 => array:2 [
          0 => "TextColumns"
          1 => "TextColumns"
        ]
        2 => array:1 [
          "component" => "TextColumns"
        ]
      ]
    ]

EDIT: Here is some source code for reference. I have header, content and footer methods. Both header and footer will always return a single array. Content however will be a multidimensional array as there could be multiple content blocks i.e.

protected static function header(): array
{
    return [
        'component' => 'TextColumns',
    ];
}

protected static function content(): array
{
    return [
        [
            'component' => 'TextColumns',
        ],
        [
            'component' => 'TextColumns',
        ],
    ];
}

Then this is all merged together in the following array. Hence I need the content blocks to be flattened so that the blocks array just contains a simple array

   dd([
        'blocks'      => [
            static::header(),
            static::content(),
            static::footer(),
        ],
    ]);

Any help would be appreciated!

Thanks

  • Does this answer your question? [Turning multidimensional array into one-dimensional array](https://stackoverflow.com/questions/8611313/turning-multidimensional-array-into-one-dimensional-array) – OMi Shah Oct 26 '22 at 09:43
  • Thanks @OMi Shah. But unfortnately all those suggestions and `Arr::collapse` give me the results I mentioned and not what I'm trying to achieve. – user1469914 Oct 26 '22 at 09:54
  • do you have control over the source array? I mean are you sure about its structure? are you sure that it will always will have a maximum depth? – hassan Oct 26 '22 at 10:01
  • @hassan I've added a edit to my initial post with the source – user1469914 Oct 26 '22 at 10:08
  • 1
    @user1469914 does the `content` method will always return a 1-depth array? or it may have more nested arrays? I mean if you are sure that the content method will only return a 1-level array you can create a custom function to merge it, if you do not know you can go with recursion to generate it – hassan Oct 26 '22 at 10:11

0 Answers0