1

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];
        }
    }
   }
6
  • Your task can be solved with 2 simple nested foreach loops. What have you tried so far? Commented May 16, 2022 at 0:00
  • for($i = 0; $i<=count($array); $i++) { foreach($array as $key => $val) { if($i==$getvalue ) { $a[] = $array[$i+1]; } else { $a[] = $array[$i]; } } } Commented May 16, 2022 at 0:17
  • @user1597430 I added what i tried Commented May 16, 2022 at 0:24
  • @user1597430 I edited and also pasted which i tried Commented May 16, 2022 at 0:42
  • @user1597430 any suggestion for me Commented May 16, 2022 at 1:15

1 Answer 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 ) );

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

1 Comment

@jarad thanks for your response. It's looking good. But as we will use repeatify function in a loop it should return only first index like [0] => Array ( [id] => 29 [name] => Testing1 )

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.