0

I have a 2D array like this:

array a: (m)x(n) 
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9

and I want to insert this row to the beginning of the array:

row b: (1)x(n) 
0 1 2 3 4 5

to get array like this:

array c:  (m+1)x(n)
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9

I tried this way:

  • Create an empty array c:(m+1)x(n)
  • Assign row b to first row of array c.
  • Assign other rows of array a to array c.

It do well but I realize that it's to long. Can I do it shorter? And how?

Antony
  • 14,900
  • 10
  • 46
  • 74
user3098638
  • 45
  • 1
  • 5

1 Answers1

0
$newArray = (0,1,2,3,4,5);
array_unshift($array, $newArray);
Bilal
  • 2,645
  • 3
  • 28
  • 40