除第一行外,每第6项Php广告

除第一行外,每第6项Php广告,php,arrays,math,Php,Arrays,Math,我需要每隔6个位置添加一个项目 所以看起来是这样的: [ //item1 //item2 //item3 //item4 //item5 //NEW ITEM HERE //item7 //item8 //item9 //item10 //item11 //NEW ITEM ] 我已经试过了: foreach($ports as $key => $port) { if($key %9 == 2) {

我需要每隔6个位置添加一个项目

所以看起来是这样的:

[
   //item1
   //item2
   //item3
   //item4
   //item5
   //NEW ITEM HERE
   //item7
   //item8
   //item9
   //item10
   //item11
   //NEW ITEM
]
我已经试过了:

foreach($ports as $key => $port)
{
    if($key %9 == 2) {
        $ports->splice($key, 0, [$ads]);
    }
}

但这没有任何作用?

使用
数组\u chunk
并将元素添加到每个子数组中:

$portsChunks = array_chunk($ports, 5); // Split array to sub-arrays of max-5 elements.

// Add new element if chunk is full length.
// Means last one will not receive new element if it's shorter than 5
array_walk($portsChunks, function (&$array) {
    if (count($array) == 5) {
        $array[] = 'New Item';
    }
});

// Use arguments unpacking to pass all chunks to array_merge
$ports = array_merge(...$portsChunk);

使用
array\u chunk
并将元素添加到每个子数组中:

$portsChunks = array_chunk($ports, 5); // Split array to sub-arrays of max-5 elements.

// Add new element if chunk is full length.
// Means last one will not receive new element if it's shorter than 5
array_walk($portsChunks, function (&$array) {
    if (count($array) == 5) {
        $array[] = 'New Item';
    }
});

// Use arguments unpacking to pass all chunks to array_merge
$ports = array_merge(...$portsChunk);

我希望下面的案例能够解决您的问题:

$newarr = array();

    $cnt = 1;


    foreach($arr as $key=>$value){

            $newarr[] = $value;

            if($cnt%5 == 0){

            $newarr[] = 'this is new item';

        }

            $cnt++;

    }
print_r($newarr);

我希望以下案例能解决您的问题:

$newarr = array();

    $cnt = 1;


    foreach($arr as $key=>$value){

            $newarr[] = $value;

            if($cnt%5 == 0){

            $newarr[] = 'this is new item';

        }

            $cnt++;

    }
print_r($newarr);
$ports=[1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,4,4,5,5,6];
$ports=阵列块($ports,5);
foreach($port as&$port){
数组_push($port,'new value');
}
unset(港口);
如果(计数($ports)[计数($ports)-1])<6){
数组_pop($port[count($port)-1]);
}
$ports=阵列合并(…$ports);
$port=[1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,2,3,3,4,5,5,6];
$ports=阵列块($ports,5);
foreach($port as&$port){
数组_push($port,'new value');
}
unset(港口);
如果(计数($ports)[计数($ports)-1])<6){
数组_pop($port[count($port)-1]);
}
$ports=阵列合并(…$ports);

您只需要一个新数组来保存所有数据。然后在旧数组上迭代,在每个第5位之后插入一个新元素。像这样的

$result = [];
$ports = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$porstCount = count($ports);
for ($i = 0; $i < $portsCount; $i++) {
    $result[] = $ports[$i];
    if ($i > 0 && $i % 5 === 0) {
        $result[] = 'new element';
    }
}

// $result will be [1, 2, 3, 4, 5, 'new element', 6, 7, 8, 9, 10, 'new element', 11, 12]
$result=[];
$ports=[1,2,3,4,5,6,7,8,9,10,11,12];
$porstCount=计数($ports);
对于($i=0;$i<$portscont;$i++){
$result[]=$ports[$i];
如果($i>0&&$i%5==0){
$result[]=“新元素”;
}
}
//$result将是[1,2,3,4,5'新元素',6,7,8,9,10'新元素',11,12]

您只需要一个新数组来保存所有数据。然后在旧数组上迭代,在每个第5位之后插入一个新元素。像这样的

$result = [];
$ports = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$porstCount = count($ports);
for ($i = 0; $i < $portsCount; $i++) {
    $result[] = $ports[$i];
    if ($i > 0 && $i % 5 === 0) {
        $result[] = 'new element';
    }
}

// $result will be [1, 2, 3, 4, 5, 'new element', 6, 7, 8, 9, 10, 'new element', 11, 12]
$result=[];
$ports=[1,2,3,4,5,6,7,8,9,10,11,12];
$porstCount=计数($ports);
对于($i=0;$i<$portscont;$i++){
$result[]=$port[$i];
如果($i>0&&$i%5==0){
$result[]=“新元素”;
}
}
//$result将是[1,2,3,4,5'新元素',6,7,8,9,10'新元素',11,12]

您可以使用
foreach
循环:

$ports = range(1,50);
$new_ports = [];
foreach ($ports as $key => $port) {
    $new_ports[] = $port;
    if(!(($key+1)%5))
        $new_ports[] = 'New item';
}
print_r($new_ports);

您可以使用
foreach
循环:

$ports = range(1,50);
$new_ports = [];
foreach ($ports as $key => $port) {
    $new_ports[] = $port;
    if(!(($key+1)%5))
        $new_ports[] = 'New item';
}
print_r($new_ports);


为什么不仅仅是
%6==0
?为什么是%6==2?它应该是%6==0!当我尝试
%6==0
时,它会启动(将其置于1位置)。第一个需要在位置6。这就是问题所在。
如果($key%6==0&&$key!=0)
那么第一项从位置7开始。为什么不干脆
%6==0
?为什么%6==2?它应该是%6==0!当我尝试
%6==0
时,它会启动(将其置于1位置)。第一个需要在位置6。这就是问题所在。
如果($key%6==0&&$key!=0)
那么第一项从位置7开始。为什么
回显
,为什么在可以使用
$key
时使用自己的键?为什么
回显
为什么在可以使用
$key
时使用自己的键?为什么不
$port=[range(1,6)、range(1,6)、range(1,6)]
@Kerkouch$a=范围(1,6)$端口=[$a,$a,$a];//[[1-6],[1-6],[1-6]]为什么不
$port=[range(1,6),range(1,6),range(1,6)]
@Kerkouch$a=范围(1,6)$端口=[$a,$a,$a];//[[1-6],[1-6],[1-6]]不
0%4==0
?@Justinas确实如此。谢谢你指点。也从4改为5,因为位置是6thDoesn不是
0%4==0
?@Justinas。谢谢你指点。也从4改为5,因为位置是6。这正是我几乎完成打字的地方。很简单,很好。但是他只有第一个元素有问题(当
$key==0
时),所以这意味着他的数组没有关联性。这正是我几乎完成键入的内容。很简单,很好。但是他只有第一个元素有问题(当
$key==0
时),所以这意味着他的数组是不关联的。