我有一个这样的循环
$rr=array();
foreach($relations as $key=>$type){
$rr[$relationType->U2U_Related_USR_ID]=$type[$k]->MSTT_Name.' / '.$type[$k+1]->MSTT_Name;
$k++;
}
我只得到第一个索引值。如何为每个连接两个索引值。
增加2!
$rr = array();
for ($i = 0, $n = count($type); $i < $n; $i += 2) {
$t1 = $type[$i];
$t2 = $type[$i + 1];
$rr[$relationType->U2U_Related_USR_ID] = $t1->MSTT_Name.' / '.$t2->MSTT_Name;
}
注意: $type
的长度应该是偶数!
您可以在循环中使用 2 对键/值,如下所示:
foreach($relations as $key=>$type){
list( $odd_key, $odd_value ) = each( $relations );
//... your code here
// This work with a step by 2 elements. If you need step by 1,
// add the following line at the end of the loop :
//prev( $relations )
}