0

Hi Good evening all

I am trying to capture the below multi dimensional array using either POST or GET. the data is passed from a form and is displayed in the array format using print_r(GET);

The packagecount = 2 indicates the mumber of packages and also corresponds to 1 and 2 items in the array. How can I pass the two dimensional array data into usable data to carry out calculations

Thanks in advance

D

Array
(
    [packagecount] => 2
    [code] => uBQnRe7ScvejacBsb7/f4Q==
    [id_coll_country] => UK
    [id_coll_postcode] => (if known)
    [id_del_country] => UK
    [id_del_postcode] => (if known)
    [weight] => Array
        (
            [1] => 3
            [2] => 10
        )

    [length] => Array



    (
            [1] => 66
            [2] => 5
        )

    [width] => Array
        (
            [1] => 68
            [2] => 5
        )

    [height] => Array
        (
            [1] => 88
            [2] => 5
        )

    [x] => 62
    [y] => 23
)
3
Contributors
7
Replies
49
Views
4 Years
Discussion Span
Last Post by paulkd
0

It all depends on what you want to do. The two items are x and y? What have you tried? A simple $_GET['width'][1] it's not enough?

0

What I wish to do is calculate the total weight of all the packages in a consignment work out the volume of the dimensions W x L x H of each box and compare weight against volume to see which is the greater.

As I have said I am passing the data from a form using GET and using print_r(GET) I would like anyone to get me on the way in part by sugesting how to solve it.

Thanks

D

0

I have placed array in a for loop up to the packagecount, is this the best way to abstract this data?

Thanks

D

echo "Package Count".$packagecount = $_GET['packagecount'];
for ($x = 1; $x<=$packagecount; $x++)
        {

            echo "<br>";
            echo "Weight".$weight = $_GET['weight'][$x];
            echo "<br>";
            echo "Width".$width = $_GET['width'][$x];
            echo "<br>";
            echo "Length".$length = $_GET['length'][$x];
            echo "<br>";
            echo "Height".$height = $_GET['height'][$x];
            echo "<br>";
        }

?>
0

For lazyness I've just updated your $_GET array

$_GET['totalWeight'] = 0;
for($i=0; $i<$_GET['packageCount']; $i++) {
    $_GET['totalWeight'] += $_GET['weight'][$i];
    $_GET['volume'][$i] = $_GET['width'][$i]*$_GET['length'][$i]*$_GET['height'][$i];
}
0

For the largest volume, are you interested in the actual largest volume or the box's index of the largest volume or both?

0

Hi Paul thanks for your help code didn't seem to work in the way you set it out, but you put me on the right track. If you want to look at your code again as always looking for a better way of writing code. echoing out the code displays the correct data and squence

Thanks again

D

$volume=0;
$TotalWeight=0;
$packagecount = $_GET['packagecount'];
for ($x = 1; $x<=$packagecount; $x++)
        {
            echo "<br>";
            echo $TotalWeight  += $_GET['weight'][$x];
            echo "<br>";
            echo $volume += $_GET['width'][$x]*$_GET['length'][$x]*$_GET['height'][$x];
        }
0

David,

Yes. I now see that your arrays are starting at 1. I created my test data with a simple array(3,10) (for weight etc) which meant that they start at 0.

Glad to help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.