skip to Main Content

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

2

Answers


  1. 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];
    }
    
    Login or Signup to reply.
  2. 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,
    ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search