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
    ]
  ]
]
  • 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. – CBroe Oct 08 '21 at 06:18
  • Sorry, i have updated - i must be John, Doe and Smith – Vaibhav Jain Oct 08 '21 at 06:34
  • Great, and now what about the second part of my question. – CBroe Oct 08 '21 at 06:35
  • array[3] is the data from 3 different excel files. Need to merge these 3 files and need the array as mentioned above – Vaibhav Jain Oct 08 '21 at 08:06

0 Answers0