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
)

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?

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

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

?>

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.