Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带条件索引的Php数组迭代_Php_Arrays_Loops_Foreach - Fatal编程技术网

带条件索引的Php数组迭代

带条件索引的Php数组迭代,php,arrays,loops,foreach,Php,Arrays,Loops,Foreach,我正在进行PHP数组迭代。例如,我有如下数组: 1) 年龄 我正在使用foreach迭代此数组: foreach($age as $index => $value) { if ($value < '18') { $banned['name_' . $index] = $value; // Push values below 18 to 'banned' array with index value } } 我想要这个新的索引,以便以后执行一些AP

我正在进行PHP数组迭代。例如,我有如下数组: 1) 年龄

我正在使用foreach迭代此数组:

foreach($age as $index => $value) {
    if ($value < '18') {
         $banned['name_' . $index] = $value; // Push values below 18 to 'banned' array with index value
    }

}
我想要这个新的索引,以便以后执行一些API调用


那么,有什么办法可以实现这一点呢

你的意思是:

$count = 0;
foreach($age as $index => $value) {
    if ($value < '18') {
         $banned[$index . '_' . $count++] = $value; // Push values below 18 to 'banned' array with index value
    }

}
$count=0;
foreach($index=>$value时的年龄){
如果($value<'18'){
$BANCED[$index.'..$count++]=$value;//将18以下的值推送到具有索引值的“BANCED”数组中
}
}

您可以这样做:

$i = 0;    
foreach($age as $index => $value) 
{
        if ($value < '18') {
             $banned['name_' . $i] = $value; // Push values below 18 to 'banned' array with index value
             $i++;
        }
}
$i=0;
foreach($index=>$value时的年龄)
{
如果($value<'18'){
$banked['name.'$i]=$value;//将18以下的值推送到具有索引值的“banked”数组
$i++;
}
}
$banked=array_filter($age,function($var){return$var['age']<18;})
$count = 0;
foreach($age as $index => $value) {
    if ($value < '18') {
         $banned[$index . '_' . $count++] = $value; // Push values below 18 to 'banned' array with index value
    }

}
$i = 0;    
foreach($age as $index => $value) 
{
        if ($value < '18') {
             $banned['name_' . $i] = $value; // Push values below 18 to 'banned' array with index value
             $i++;
        }
}
$i = 0;

foreach ($age as $name => $age)
{
    $banned[$name . '_' . $i++] = $age;
}