Clover
Clover

Reputation: 5

How to duplicate every key in php array

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

Upvotes: 0

Views: 72

Answers (1)

Delmontee
Delmontee

Reputation: 2364

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

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

print_r( $newarray);

?

Upvotes: 1

Related Questions