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];
}
}
}