如果键不相同,如何取消设置数组关联多维

时间:2016-09-08 02:58:33

标签: php arrays multidimensional-array

我有这样的数据:

[1] => Array
    (
        [COMPANY_SERVICE_ID] => CS01
        [COMPANY_NAME] => HANOMAN SAKTI PRATAMA, PT - JAKARTA
        [TARIFF_CURRENCY] => IDR
        [SELLING_SERVICE_ID] => SS01
        [CONTAINER_TYPE_ID] => DC
        [SERVICE_NAME] => CONTAINER TRUCKING SERVICE
        [FROM_QTY] => 1
        [TO_QTY] => 100
        [FROM_NAME] => TANJUNG PRIOK
        [FROM_LOCATION_ID] => L096
        [TO_NAME] => BALARAJA
        [TO_LOCATION_ID] => L002
        [RESULT_LOCATION] => Array
            (
                [L001] => Array
                    (
                        [TARIF_20] => 1.500.000,00
                        [TARIF_40] => 1.750.000,00
                        [TARIF_45] => 5.500.000,00
                        [TARIF_4H] => 3.500.000,00
                    )

                [L002] => Array
                    (
                        [TARIF_20] => 500.000,00
                    )

            )

    )

如果RESULT_LOCATION中的值与TO_LOCATION_ID的值不同,我想取消设置RESULT_LOCATION的值。

我该怎么办?

我尝试使用以下代码删除:

foreach ($hasil_jakarta as $key => $value) {
        foreach ($value['RESULT_LOCATION'] as $key1 => $value1) {
            if ($value['TO_LOCATION_ID'] != $value['RESULT_LOCATION'][$key1]) {
                unset($hasil_jakarta[$key]['RESULT_LOCATION'][$key1]);
            }
        }
    }

但删除了RESULT_LOCATION的所有值。我的代码怎么了?

1 个答案:

答案 0 :(得分:2)

您不必测试值,而是键。然后,unset()数组具有不同的键。

以下是一个例子:

<?php
$a = array(
        'COMPANY_SERVICE_ID' => 'CS01',
        'COMPANY_NAME' => 'HANOMAN SAKTI PRATAMA, PT - JAKARTA',
        'TARIFF_CURRENCY' => 'IDR',
        'SELLING_SERVICE_ID' => 'SS01',
        'CONTAINER_TYPE_ID' => 'DC',
        'SERVICE_NAME' => 'CONTAINER TRUCKING SERVICE',
        'FROM_QTY' => 1,
        'TO_QTY' => 100,
        'FROM_NAME' => 'TANJUNG PRIOK',
        'FROM_LOCATION_ID' => 'L096',
        'TO_NAME' => 'BALARAJA',
        'TO_LOCATION_ID' => 'L002',
        'RESULT_LOCATION' => array(
                'L001' => array(
                        'TARIF_20' => '1.500.000,00',
                        'TARIF_40' => '1.750.000,00',
                        'TARIF_45' => '5.500.000,00',
                        'TARIF_4H' => '3.500.000,00'
                ),
                'L002' => array(
                        'TARIF_20' => '500.000,00'
                )
        )
);


foreach ($a['RESULT_LOCATION'] as $key => $value) {
    if ($key != $a['TO_LOCATION_ID']) {
        unset($a['RESULT_LOCATION'][$key]);
    }
}

结果将是:

php > print_r($a);
Array
(
    [COMPANY_SERVICE_ID] => CS01
    [COMPANY_NAME] => HANOMAN SAKTI PRATAMA, PT - JAKARTA
    [TARIFF_CURRENCY] => IDR
    [SELLING_SERVICE_ID] => SS01
    [CONTAINER_TYPE_ID] => DC
    [SERVICE_NAME] => CONTAINER TRUCKING SERVICE
    [FROM_QTY] => 1
    [TO_QTY] => 100
    [FROM_NAME] => TANJUNG PRIOK
    [FROM_LOCATION_ID] => L096
    [TO_NAME] => BALARAJA
    [TO_LOCATION_ID] => L002
    [RESULT_LOCATION] => Array
        (
            [L002] => Array
                (
                    [TARIF_20] => 500.000,00
                )

        )

)

希望它有所帮助。

修改

我看到了你的片段并根据你的需要调整了循环:

foreach ($hasil_jakarta as $h_j_key => $h_j_value) {
    foreach ($h_j_value['RESULT_LOCATION'] as $key => $value) {
        if ($key != $h_j_value['TO_LOCATION_ID']) {
            unset($hasil_jakarta[$h_j_key]['RESULT_LOCATION'][$key]);
        }
    }
}

你的代码几乎不错,但未设置的代码不是。在这种情况下,您需要从$hasil_jakarta数组中取消设置变量。

祝你好运!