1

I have an existing array, example:

Array
(
    [0] => ABCD EFGH
    [1] => 123456
    [2] => 7890
)

Now I have an existing field with three values. I would need to split the array with the first value, ie Key [0], and update the existing array with new array values by inserting it at the beginning of the array. Desired output:

Array
(
    [0] => ABCD
    [1] => EFGH
    [2] => 123456
    [3] => 7890
)

i wanted to do an explode () of an existing array [0] explode(" ", $array[0]);

and the newly created two array using the function array_push(); But thearray_push(); function allows to update the original array by inserting a new value only at the end of the array. Is there an easy way to do this? Or do I have to build a new array and extend it with new values by overwriting the entire array?

Thank you very much.

Petr Fořt Fru-Fru
  • 858
  • 2
  • 8
  • 23

2 Answers2

0

Use array_unshift instead of array_push

0

Data Array

Array
(
    [0] => ABCD EFGH
    [1] => 123456
    [2] => 7890
)

    $newArr = array();
    foreach ($data as $key => $res) {
        $exRes = explode(" ", $res);
        foreach ($exRes as $ind => $ex) {
            array_push($newArr, $ex);
        }
    } 

Result like this

Array
(
    [0] => ABCD
    [1] => EFGH
    [2] => 123456
    [3] => 7890
)
Vinay Kaklotar
  • 424
  • 4
  • 11