连续的两个索引值



我有一个像这样的循环

$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 )
}

最新更新