Jump to content

adding values in array


mainstreetop

Recommended Posts

I have the following multidimensional array where each sub-array represents quantity[0] and item #[1]:

  Array

(

    [0] => Array

        (

            [0] => 1

            [1] => 12113

        )

 

    [1] => Array

        (

            [0] => 2

            [1] => 21200

        )

 

    [2] => Array

        (

            [0] => 4

            [1] => 10200

        )

 

    [3] => Array

        (

            [0] => 1

            [1] => yyyy

        )

 

    [4] => Array

        (

            [0] => 20

            [1] => xxxxx

        )

 

    [5] => Array

        (

            [0] => 5

            [1] => 21200

        )

 

I'm trying to consolidate like items.  For example I need to create a new array that reflects any duplicate part numbers with a combined quantity.

(ie.

...

[0] => 7

[1] => 21200

...

 

Link to comment
Share on other sites

@ huggiebear -

 

Sorry about that and thanks for responding.  This is somewhat new to me.  Hopefully, this looks a little better.

 

  [0] Array  => 0    (  
       [0] => 1
       [1] => 12113 )

  [1] => Array        (
       [0] => 2
       [1] => 21200 )

  [2] => Array        (
       [0] => 4
       [1] => 10200 )

  [3] => Array        (
       [0] => 1
       [1] => yyyy    )

  [4] => Array        (
       [0] => 20
       [1] => xxxxx   )

  [5] => Array        (
       [0] => 5
       [1] => 21200  )

 

Link to comment
Share on other sites

I created it like that.  The idea is to grab quantities and item numbers entered in an order form.  So, I created an array of quantities and an array of item numbers.  I then created array containing both sub-arrays.

 

Now, I want to validate the item numbers against the database and insert the data into the 'cart' table.

 

However, I'm trying to consolidate like items in the vent the user enters the same item number more than once.

 

Hopefully, that made sense.

Link to comment
Share on other sites

It makes perfect sense, but I'm sure there's a better way to do it.  Here's an interim solution:

<?php
// Declare totals as an array
$totals = array();

// This is the format of your array (I assume posted from your form) notice some duplicates
$submitted[] = array('1', '12113');
$submitted[] = array('2', '21200');
$submitted[] = array('4', '10200');
$submitted[] = array('1', 'yyyy');
$submitted[] = array('20', 'xxxxx');
$submitted[] = array('5', '21200');
$submitted[] = array('7', '21200');

// This sticks it into a new array keyed on product id with quantity as a value
foreach ($submitted as $k => $a){
    if (!array_key_exists($submitted[$k][1], $totals)){
        $totals[$submitted[$k][1]] = $submitted[$k][0];
    }
    else {
        $totals[$submitted[$k][1]] = $totals[$submitted[$k][1]] + $submitted[$k][0];
    }
}

// Print the array and total quantities
echo '<pre>';
print_r($totals);
echo '</pre>';

?>

Link to comment
Share on other sites

Nothing wrong with your solution, but the key really isn't necessary for the example provided (hmm, pet peeve, maybe.)

 

<?php
$submitted = array(
array('1', '12113'),
array('2', '21200'),
array('4', '10200'),
array('1', 'yyyy'),
array('20', 'xxxxx'),
array('5', '21200'),
array('7', '21200')
);

$finalArr = array();

foreach ($submitted as $dataArr){
$finalArr["{$dataArr['1']}"] = (isset($finalArr["{$dataArr['1']}"])) ? $finalArr["{$dataArr['1']}"] + $dataArr[0] : $dataArr[0];
}
?>

Link to comment
Share on other sites

I thought the additional key was necessary. 

 

If I were to create a single array using quantity => item# as the $k => $v, then the array will rearrange itself because of duplicate keys if the user enters the same quantity for two or more items.

 

Or, maybe I do not fully understand which key is superfluous.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.