json PHP切换大小写以更新数组而不影响数组

waxmsbnn  于 2023-03-09  发布在  PHP
关注(0)|答案(2)|浏览(94)

我正在为一家小型企业开发一个与FedEx/UPS货运标签创建者的API集成。我更习惯于Python,所以我在PHP方面遇到了一些障碍,但这是迄今为止我唯一无法解决的问题。对于包含多个包裹的货运,每个包裹都必须有自己的价值、长度、宽度、高度和重量。而且还必须有一个货物的总价值。我做了一个开关案例来计算总价值(因为数据是以特定于业务的项目代码发送的),这工作得很好。但是现在,尝试使用相同的开关案例来更新API调用的有效负载是行不通的。
这是我现在的情况:

for ($j=1; $j<=$_POST['package_count']; $j++) {
        $payload['package'.$j] = [];
        switch ($_POST['insurance'.$j]) {
        case '572':
            $value += 100;
            $payload['package'.$j]['value'] = 100;
            break;
        case '248':
            $value += 500;
            $payload['package'.$j]['value'] = 500;
            break;
        case '544':
            $value += 1000;
            $payload['package'.$j]['value'] = 1000;
            break;
        case '569':
            $value += 1500;
                    $payload['package'.$j]['value'] = 1500;
            break;
        case '570':
            $value += 2000;
            $payload['package'.$j]['value'] = 2000;
            break;
        case '571':
            $value += 2500;
            $payload['package'.$j]['value'] = 2500;
            break;
    }
    $totalWeight += $_POST['weight'.$j];
    $payload['package'.$j] = ['weight' => $_POST['weight'.$j], 'length' => $_POST['length'.$j], 'width' => $_POST['width'.$j], 'height' => $_POST['height'.$j]];
}
echo print_r($payload);

$value更新正确,但当我得到有效负载的回应时,包看起来像:

[package1] => Array
        (
            [weight] => 1
            [length] => 2
            [width] => 3
            [height] => 2
        )

[package2] => Array
        (
            [weight] => 1
            [length] => 2
            [width] => 3
            [height] => 2
        )

关于为什么有效负载packageJ子数组没有得到'value'的线索吗?

snvhrwxg

snvhrwxg1#

在每次迭代的最后,您将覆盖在switch/case中设置的值。
如果要我猜的话,你需要weight/length/width/height属性的值,如果是这样的话,那么把赋值从迭代的末尾移到开始:

for ($j=1; $j<=$_POST['package_count']; $j++) {
    $payload['package'.$j] = ['weight' => $_POST['weight'.$j], 'length' => $_POST['length'.$j], 'width' => $_POST['width'.$j], 'height' => $_POST['height'.$j]];
    switch ($_POST['insurance'.$j]) {
        case '572':
            $value += 100;
            $payload['package'.$j]['value'] = 100;
            break;
        // etc...
    }
    $totalWeight += $_POST['weight'.$j];
}
flseospp

flseospp2#

在以下行中创建一个新数组:

$payload['package'.$j] = ['weight' => $_POST['weight'.$j], 'length' => $_POST['length'.$j], 'width' => $_POST['width'.$j], 'height' => $_POST['height'.$j]];

改为:

$payload['package'.$j]['weight'] = $_POST['weight'.$j];
$payload['package'.$j]['length'] = $_POST['length'.$j];
$payload['package'.$j]['width'] = $_POST['width'.$j];
$payload['package'.$j]['height'] = $_POST['height'.$j];

$packageValue = 0;     
switch ($_POST['insurance'.$j]) {
    case '572':
        $value += 100;
        //$payload['package'.$j]['value'] = 100;
        $packageValue = 100;
        break;
    case '248':
        $value += 500;
        //$payload['package'.$j]['value'] = 500;
        $packageValue = 500;
        break;
      ...

然后:

$payload['package'.$j] = [
    'weight' => $_POST['weight'.$j], 
    'length' => $_POST['length'.$j], 
    'width' => $_POST['width'.$j], 
    'height' => $_POST['height'.$j],
    'value' => $packageValue,
];

相关问题