1

Merge a multi-dimensional array to the custom array as required. Need the solution in PHP (using in Laravel-8). Custom array is needed to make rows for creating an excel file using Spreadsheet. This is the Original array I have =

array:3 [
  0 => array:4 [
    0 => array:3 [
      0 => "Name"
      1 => "English"
      2 => "Math"
    ]
    1 => array:3 [
      0 => "John"
      1 => 10
      2 => 20
    ]
    2 => array:3 [
      0 => "Doe"
      1 => 20
      2 => 30
    ]
    3 => array:3 [
      0 => "Smith"
      1 => 30
      2 => 50
    ]
  ]
  1 => array:4 [
    0 => array:3 [
      0 => "Name"
      1 => "Science"
      2 => "Hindi"
    ]
    1 => array:3 [
      0 => "John"
      1 => 10
      2 => 20
    ]
    2 => array:3 [
      0 => "Doe"
      1 => 20
      2 => 57
    ]
    3 => array:3 [
      0 => "Smith"
      1 => 30
      2 => 89
    ]
  ]
  2 => array:4 [
    0 => array:3 [
      0 => "Name"
      1 => "ABC"
      2 => "XYZ"
    ]
    1 => array:3 [
      0 => "John"
      1 => 10
      2 => 20
    ]
    2 => array:3 [
      0 => "Doe"
      1 => 20
      2 => 23
    ]
    3 => array:3 [
      0 => "Smith"
      1 => 30
      2 => 89
    ]
  ]
]

From the above array need to make the array like below (array size may very, so need dynamic solutions) -

array:1 [
  0 => array:4 [
    0 => array:7 [
      0 => "Name"
      1 => "English"
      2 => "Math"
      3 => "Science"
      4 => "Hindi"
      5 => "ABC"
      6 => "XYZ"
    ]
    1 => array:7 [
      0 => "John"
      1 => 10
      2 => 20
      3 => 10
      4 => 20
      5 => 10
      6 => 20
    ]
    2 => array:7 [
      0 => "Doe"
      1 => 20
      2 => 30
      3 => 20
      4 => 57
      5 => 20
      6 => 23
    ]
    3 => array:7 [
      0 => "Smith"
      1 => 30
      2 => 50
      3 => 30
      4 => 89
      5 => 30
      6 => 89
    ]
  ]
]
4
  • Neither Assamese nor Vaibhav nor Fardeen nor Partha appear to exist in your input data, so where are those coming from? Also, what have you tried so far, and what specifically was the problem with it? This site is not a code-writing service, so you should present an attempt at least. Commented Oct 8, 2021 at 6:18
  • Sorry, i have updated - i must be John, Doe and Smith Commented Oct 8, 2021 at 6:34
  • Great, and now what about the second part of my question. Commented Oct 8, 2021 at 6:35
  • array[3] is the data from 3 different excel files. Need to merge these 3 files and need the array as mentioned above Commented Oct 8, 2021 at 8:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.