0

How to add an duplicate array that after every items in array parent, the same array item is added.

The array is :

Array
(
    [0] => Array
        (
            [0] => zero
        )

    [1] => Array
        (
            [0] => one
        )
        
)

Result expectation :

Array
(
    [0] => Array
        (
            [0] => zero
        )

    [1] => Array
        (
            [0] => zero
        )

    [2] => Array
        (
            [0] => one
        )

    [3] => Array
        (
            [0] => one
        )

)

Please provide me with the proper code. Any help will be appreciated <3

2
  • 2
    What have you tried so far? Where are you stuck? Commented Aug 5, 2021 at 12:43
  • 2
    Why not $array = array_merge($array, $array);? Commented Aug 5, 2021 at 13:08

1 Answer 1

1
$originalarray = ["zero","one","two"];
$newarray = [];

foreach($originalarray as $key=>$val){
    array_push($newarray,$val);
    array_push($newarray,$val);
}

print_r( $newarray);

?

Sign up to request clarification or add additional context in comments.

Comments

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.