Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP switch case to update array not affecting array

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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’?

>Solution :

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];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading