数组PHP中的条件

时间:2013-12-01 17:38:07

标签: php arrays conditional

我很困惑如何编写适合我的问题的标题。好吧,我有两个如下的阵列:

第一个数组叫做“mahasiswa”

Array
(
    [0] => Array
        (
            [npm] => 1123123123
            [nama] => LINDA 
        )

    [1] => Array
        (
            [npm] => 121323131
            [nama] => SETIADI
        )

    [2] => Array
        (
            [npm] => 12312312
            [nama] => WIWIN
        )

    [3] => Array
        (
            [npm] => 12345678910
            [nama] => ABDUL IMAN
        )

    [4] => Array
        (
            [npm] => 12355141
            [nama] => KOKOM
        )

    [5] => Array
        (
            [npm] => 123U4148U90
            [nama] => JIHAN 
        )

    [6] => Array
        (
            [npm] => 978687999
            [nama] => KURNIA 
        )
)

这被称为“piutang”

Array
(
    [0] => Array
        (
            [total] => 14600000
            [nama] => KOKOM
            [npm] => 12355141
        )

    [1] => Array
        (
            [total] => 8000000
            [nama] => KURNIA
            [npm] => 978687999
        )

)

现在我必须对两个数组进行调节,所以我可以将它们组合成一个新数组......这是我的代码示例:

$i = 0;
$rekappiutang = array();
foreach ($mahasiswa as $row) {
   foreach ($piutang as $key=>$val) {
      if ($val['npm'] == $row['npm']) {
         $rekappiutang[$i]['npm']  = $row['npm'];
         $rekappiutang[$i]['nama'] = $row['nama'];
         $rekappiutang[$i]['totalpiutang']  = $val['total'];
       } else {
         $rekappiutang[$i]['npm']  = $row['npm'];
         $rekappiutang[$i]['nama'] = $row['nama'];
         $rekappiutang[$i]['totalpiutang'] = 0;
       }                  
   }
   $i++;
}

上面代码的结果应该是这样的:

Array
(
    [0] => Array
        (
            [npm] => 1123123123
            [nama] => LINDA 
            [totalpiutang] => 0
        )

    [1] => Array
        (
            [npm] => 121323131
            [nama] => SETIADI
            [totalpiutang] => 0
        )

    [2] => Array
        (
            [npm] => 12312312
            [nama] => WIWIN
            [totalpiutang] => 0
        )

    [3] => Array
        (
            [npm] => 12345678910
            [nama] => ABDUL IMAN
            [totalpiutang] => 0
        )

    [4] => Array
        (
            [npm] => 12355141
            [nama] => KOKOM
            [totalpiutang] => 14600000
        )

    [5] => Array
        (
            [npm] => 123U4148U90
            [nama] => JIHAN 
            [totalpiutang] => 0
        )

    [6] => Array
        (
            [npm] => 978687999
            [nama] => KURNIA 
            [totalpiutang] => 8000000
        )
)

但我总是得到这个输出:

Array
(
    [0] => Array
        (
            [npm] => 1123123123
            [nama] => LINDA 
            [totalpiutang] => 0
        )

    [1] => Array
        (
            [npm] => 121323131
            [nama] => SETIADI
            [totalpiutang] => 0
        )

    [2] => Array
        (
            [npm] => 12312312
            [nama] => WIWIN
            [totalpiutang] => 0
        )

    [3] => Array
        (
            [npm] => 12345678910
            [nama] => ABDUL IMAN
            [totalpiutang] => 0
        )

    [4] => Array
        (
            [npm] => 12355141
            [nama] => KOKOM
            [totalpiutang] => 0
        )

    [5] => Array
        (
            [npm] => 123U4148U90
            [nama] => JIHAN 
            [totalpiutang] => 0
        )

    [6] => Array
        (
            [npm] => 978687999
            [nama] => KURNIA 
            [totalpiutang] => 8000000
        )
)

值为“KOKOM”的数据始终为空,我如何修复它们?谢谢

1 个答案:

答案 0 :(得分:2)

问题是你的索引,每次你进入第二个foreach你覆盖索引位置,例如,如果你在第一次迭代中你的索引将是0,在第二次迭代你的索引仍然是0和in在这种情况下,你将丢失你在第一次迭代中收集的信息,你可以做两件事,这取决于你想要如何调整你的数组,你可以在第一个foreach之外突破,如果你找到了什么(如果这是你想要获得的)像这样:

if ($val['npm'] == $row['npm']) {
     $rekappiutang[$i]['npm']  = $row['npm'];
     $rekappiutang[$i]['nama'] = $row['nama'];
     $rekappiutang[$i]['totalpiutang']  = $val['total'];
    break;
}

或者你可以在第一个foreach的末尾递增索引,从而存储所有结果而不会覆盖。

KURNIA的作用只是因为它是数组的最后一个元素,之后foreach会自动中断。