PHP切换情况更新数组不影响数组

huangapple go评论71阅读模式
英文:

PHP switch case to update array not affecting array

问题

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

I'm working on developing an API integration for a small business with FedEx/UPS shipping label creators. I'm more accustomed with Python, so I've hit a few stumbling blocks with PHP, but this is the only one I haven't been able to work out so far. For shipments with multiple packages, each package has to have its individual value, length, width, height, and weight assigned, and there has to be a total value for the shipment as well. I made a switch case for tallying the total value (because the data is sent in a business-specific item code), and that worked fine. But now, trying to use that same switch case to update the payload for the API call isn't working.

Here's what I have right now:

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);

The $value updates correctly, but when I get the echo of the payload, the packages look like:

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

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

Any clues as to why the payload packageJ subarray isn't getting the 'value'?

答案1

得分: 1

在每次迭代的最后,你都在覆盖在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], 'value' => 0];  // 将value属性的赋值移到这里
    switch ($_POST['insurance'.$j]) {
        case '572':
            $value += 100;
            $payload['package'.$j]['value'] = 100;
            break;
        // 等等...
    }
    $totalWeight += $_POST['weight'.$j];
}
英文:

At the very end of each iteration you are overriding the value that you are setting in your switch/case.

If I had to guess, you want the weight/length/width/height properties and the value. If that is the case, then move the assignment from the end of the iteration to the beginning:

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];
}

答案2

得分: 1

在这一行中,你创建了一个新的数组:

$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;
    // 其他情况...
}

然后:

```php
$payload['package'.$j] = [
    'weight' => $_POST['weight'.$j], 
    'length' => $_POST['length'.$j], 
    'width' => $_POST['width'.$j], 
    'height' => $_POST['height'.$j],
    'value' => $packageValue,
];
英文:

You create a new array in this line:

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

Replace it by:

$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];

OR

$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;
      ...

And then:

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

huangapple
  • 本文由 发表于 2023年3月7日 11:20:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定