PHP 数组中的条件

问题描述 投票:0回答:2

我很困惑该写什么适合我的问题的标题。好吧,我有两个如下所示的数组:

第一个数组称为“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”的数据始终为空,我如何修复它们?谢谢你

php arrays conditional-statements
2个回答
2
投票

问题是你的索引,每次你进入第二个 foreach 时,你都会覆盖索引位置,例如,如果你进入第一次迭代,你的索引将为 0,在第二次迭代中,你的索引仍将为 0,在这种情况下您将丢失在第一次迭代中收集的信息,您可以根据您想要如何调节数组来执行两件事,如果您找到像这样的东西(如果这就是您想要获得的内容),您可以突破第一个 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 会自动中断。


0
投票

进行基于值的搜索是将一个数组的值映射到另一个数组的效率最低的方法之一。

相反,创建一个查找映射,其中 nama 值作为键,总计值作为值。 然后迭代主数组并将找到的值或默认的 0 值附加到每行。 演示

$map = array_column($piutang, 'nama', 'total');

var_export(
    array_map(
        fn($row) => $row + ['total' => $map[$row['nama']] ?? 0],
        $mahasiswa
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.