'Get same index value of an array repeated time, based on a variable in php

We have an array in PHP like below:

Array
(
    [0] => Array
        (
            [id] => 29
            [name] => Testing1
        )
    [1] => Array
        (
            [id] => 30
            [name] => Testing2
        )
    [2] => Array
        (
            [id] => 31
            [name] => Testing3
        )
)

We also have a variable $getvalue . We want to create a function (we will use the function in for loop) in which we pass above array and $getvalue. If $getvalue = 2 it should return key[0] 2 time, key[1] 2 time and key[2] 2 time like below.

       [0] => Array
        (
            [id] => 29
            [name] => Testing1
        )
        [0] => Array
        (
            [id] => 29
            [name] => Testing1
        )
        [1] => Array
        (
            [id] => 30
            [name] => Testing2
        )
        [1] => Array
        (
            [id] => 30
            [name] => Testing2
        )
        [2] => Array
        (
            [id] => 31
            [name] => Testing3
        )
        [2] => Array
        (
            [id] => 31
            [name] => Testing3
        )

If $getvalue = 1 it should return key[0] 1 time, key[1] 1 time and key[2] 1 time like below.

   [0] => Array
    (
        [id] => 29
        [name] => Testing1
    )
    [1] => Array
    (
        [id] => 30
        [name] => Testing2
    )
   [2] => Array
    (
        [id] => 31
        [name] => Testing3
    )

Tried :

for($i = 0; $i<=count($array); $i++) {

    foreach($array as $key => $val) {

        if($i==$getvalue )
        {
            $a[] = $array[$i+1];
        }
        else
        {
            $a[] = $array[$i];
        }
      }
    } 

Also tried :

static function abc ($array,$getvalue)
   {
      foreach ($array as $key => $value) {
        for ($i=0; $i <= $getvalue; $i++) { 
            return $arr[$i];
        }
    }
   }


Solution 1:[1]

Each element of input array is added to the return value $getvalue times:

<?php
function repeatify( array $array, int $getvalue ): array {

    $out = [];
    if($getvalue <= 0) return $out;

    foreach( $array as $element ){
        foreach( range(0, $getvalue - 1) as $we_actualy_are_not_using_this_var )
            $out[] = $element;

        # Or alternatively with a `for` loop (i'll comment this out since i prefer an above code):
        /*
        for (
            $we_actualy_are_not_using_this_var = 0; 
            $we_actualy_are_not_using_this_var < $getvalue; 
            $we_actualy_are_not_using_this_var++
        ) { 
            $out[] = $element;
        }
        */
    }

    return $out;
}


$data = [
    [
        'id' => 29,
        'name' => 'Testing1',
    ],
    [
        'id' => 30,
        'name' => 'Testing2',       
    ],
    [
        'id' => 31,
        'name' => 'Testing2',       
    ],
];

print '<pre>';

print_r( repeatify( $data, 0 ) );
print_r( repeatify( $data, 1 ) );
print_r( repeatify( $data, 2 ) );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jared