Jump to content

PHP Combine Array


John84

Recommended Posts

Hi

I have an array which looks a little like this, the first number in each array is a quantity

$cars = array (
  array(1,"BMW",15,13),
  array(8,"BMW",15,13),
  array(3,"Saab",5,2),
  array(4,"BMW",11,7),
  array(5,"Land Rover",17,15),
  array(6,"Saab",5,2)
);

what I want to do is combine any array which matches another, but ignoring the quanitity value (first one).

Then creating the array again and adding up the quanitity values and re-inserting them at the start.

Therefore the end result would look like

$cars = array (
  array(9,"BMW",15,13),
  array(9,"Saab",5,2),
  array(4,"BMW",11,7),
  array(5,"Land Rover",17,15),
);

Has anyone got any ideas on how I might do this?

Thanks

Edited by John84
Link to comment
Share on other sites

6 minutes ago, Barand said:

Going back a step, how are you creating that original array?

Hi, thanks for response.

The original array is created from a bunch of posted input values, that looks something like

<input type="text" name="cars[]" value="1,BMW,15,13">
<input type="text" name="cars[]" value="8,BMW,15,13">
<input type="text" name="cars[]" value="3,Saab,5,2">

Annoyingly I dont have access to change any of the script which produces those inputs, all i can do is post them to my script

Edited by John84
Link to comment
Share on other sites

Given that your form would look something like this

image.png.d5fa8bd5412970d5ab72719111c91928.png

your cars array would be an array of string values and not an array of arrays as you posted., IE

$cars = [
            0 => "1,'BMW',15,13",
            1 => "8,'BMW',15,13",
            2 => "3,'Saab',5,2",
            3 => "3,930,2,8",
            4 => "6,370,7,1"
        ];

unless you are doing further processing to creat the posted array

Link to comment
Share on other sites

6 minutes ago, Barand said:

Given that your form would look something like this

image.png.d5fa8bd5412970d5ab72719111c91928.png

your cars array would be an array of string values and not an array of arrays as you posted., IE


$cars = [
            0 => "1,'BMW',15,13",
            1 => "8,'BMW',15,13",
            2 => "3,'Saab',5,2",
            3 => "3,930,2,8",
            4 => "6,370,7,1"
        ];

unless you are doing further processing to creat the posted array

Yes very sorry, feeling a bit stupid now for my dumb example 😀

If I do a var_dump of the array then it comes out like

  0 => string '1,930,9',
  1 => string '2,370,3,4',
  2 => string '6,930,9',
  3 => string '3,930,2',
  4 => string '8,370,2,1'

so in that example, it would produce

  0 => string '7,930,9',
  1 => string '2,370,3,4',
  2 => string '3,930,2',
  3 => string '8,370,2,1'

 

Link to comment
Share on other sites

12 minutes ago, Barand said:

try


$temp = [];

foreach ($cars as $car) {
    $qty = intval($car);
    $key = trim(strstr($car, ','), ',');
    if (!isset($temp[$key])) $temp[$key] = 0;
    $temp[$key] += $qty;
}

foreach ($temp as $k => $t) {
    $newcars[] = "$t,$k";
}

 

WOW! That looks superb! I'll give it a try then let you know, thank you

Link to comment
Share on other sites

2 hours ago, Barand said:

try


$temp = [];

foreach ($cars as $car) {
    $qty = intval($car);
    $key = trim(strstr($car, ','), ',');
    if (!isset($temp[$key])) $temp[$key] = 0;
    $temp[$key] += $qty;
}

foreach ($temp as $k => $t) {
    $newcars[] = "$t,$k";
}

 

Seems to work very well, you are a life saver, thank you for all the help given

 

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.