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



我有这样的数据:

[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的所有值。我的代码有什么问题?

不需要测试值,只需要测试键。然后,使用不同键的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数组中取消设置一个变量。

祝你好运!

相关内容

  • 没有找到相关文章

最新更新