带条件索引的Php数组迭代
我正在进行PHP数组迭代。例如,我有如下数组: 1) 年龄 我正在使用foreach迭代此数组:带条件索引的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
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;
}