0

I got the following array.

  [0]=>
    array(7) {
        [0]=>31
        [1]=>14
        [2]=>on
        [3]=>receiver
        [4]=>pollin
        [5]=>0000-e8-de-27-176d10
        [6]=>1
    }
  [1]=>
    array(7) {
        [0]=>31
        [1]=>14
        [2]=>on
        [3]=>receiver
        [4]=>pollin
        [5]=>0000-e8-de-27-176d10
        [6]=>5
    }
  [2]=>
    array(7) {
        [0]=>0
        [1]=>17
        [2]=>"on"
        [3]=>"receiver"
        [4]=>"elro_400"
        [5]=>"0000-e8-de-27-176d10"
        [6]=>1
    }

I'm trying to sum the value of [6] together, but only if the value of key 0-5 are the same. so the expected result would be this:

  [0]=>
    array(7) {
        [0]=>31
        [1]=>14
        [2]=>on
        [3]=>receiver
        [4]=>pollin
        [5]=>0000-e8-de-27-176d10
        [6]=>6
    }
  [1]=>
    array(7) {
        [0]=>0
        [1]=>17
        [2]=>"on"
        [3]=>"receiver"
        [4]=>"elro_400"
        [5]=>"0000-e8-de-27-176d10"
        [6]=>1
    }

I tried it myself with if(array_key_exists($vals[0]&&$vals[1]&&$vals[2]&&$vals[3]&&$vals[4]&&$vals[5],$res)) however this is not working like I thought it would work.

EDIT:

What I'm trying to do is remove duplicates if [0] - [5] are the same. But when it removes a duplicate it should sum [6] together.

Simple Example:

  [0]=>
    array(3) {
        [0]=>31
        [1]=>14
        [2]=>1
    }
  [1]=>
    array(3) {
        [0]=>31
        [1]=>14
        [2]=>4
    }
  [2]=>
    array(3) {
        [0]=>3
        [1]=>18
        [2]=>1
    }

If [0] and [1] are the same, then it well remove the duplicates but add together [2]

So that would result in:

  [0]=>
    array(3) {
        [0]=>31
        [1]=>14
        [2]=>5
    }
  [1]=>
    array(3) {
        [0]=>3
        [1]=>18
        [2]=>1
    }
2
  • 1
    there's always a foreach() loop Commented Jul 5, 2015 at 23:06
  • I do not understand how the value of key 6 is calculated. Would not you want to store it in a separate variable? Commented Jul 5, 2015 at 23:07

2 Answers 2

3
$data = // your dataset above
$result = array();

foreach ($data as $keyA => $valueA){
    foreach ($data as $keyB => $valueB){
        // showing an explicit way of comparing values, eventually you could use php array_diff or array_diff_key functions
        if ($valueA[0] == $valueB[0] &&
            $valueA[1] == $valueB[1] &&
            $valueA[2] == $valueB[2] &&
            $valueA[3] == $valueB[3] &&
            $valueA[4] == $valueB[4] &&
            $valueA[5] == $valueB[5] &&
            $keyA != $keyB){

            $valueA[6] += $valueB[6];
            $result[] = $valueA;
        }
    }
}

var_dump($result); // is now your expected result

Update: as noted by @vonUbisch, this would only result in the duplicate items. Non-duplicates would be ignored. Perhaps you can explain your final goal, this logic seems really awkard and maybe other approaches could offer a better solution.

Update 2:

Still seems as non optimal logic but here it goes

$data = array(
    array( 31, 14, 1),
    array( 31, 14, 4),
    array( 3, 18, 1)
);

$mutant = array();
$result = array();

// deconstruct the data set
foreach ($data as $key => $value){
    // use the first values as an 'id'
    $id = $value[0] . '.' . $value[1];

    // now we use that id as a key in the result array
    if (array_key_exists($id, $mutant)){
        // if the key exists (a previous dataset with same values existed), we add the last item
        $mutant[$id] += $value[2];
    }else{
        // if the key does not exist, we set the last item
        $mutant[$id] = $value[2];
    }
}

var_dump($mutant);

// array (size=2)
//  '31.14' => int 5
//  '3.18' => int 1

// now we rebuild it
foreach ($mutant as $key => $value){
    // split the key into original values
    $items = explode('.', $key);
    // depending on your data, you might need to iterate over $items here to cast them as ints
    // push the last value 
    $items[] = $value;
    // push sub array into result set
    $result[] = $items;
}

var_dump($result);
// array (size=2)
//   0 => 
//     array (size=3)
//       0 => string '31' * note that value is a string!
//       1 => string '14' (length=2)
//       2 => int 5
//   1 => 
//     array (size=3)
//       0 => string '3' (length=1)
//       1 => string '18' (length=2)
//       2 => int 1
Sign up to request clarification or add additional context in comments.

3 Comments

Your result will not match his, look at OP's $data[1][6], but this may be completely irrelevant.
Sorry, maybe I did not explain it properly. I'm trying to merge the duplicates together, let my give a simple example. I'm not abel to use enters in comments so I posted the example here: pastebin.com/SU5YHuTJ
@Dr.Banana check the second update and let us know if this meets your needs
1

Try

$array = [
    ...
];

$resultArray = [];
$length = count($array);

for ($i = 0; $i < $length; $i++)
{
    if (!array_key_exists($i, $array))
    {
        continue;
    }

    $array1 = $array[$i];
    $values1 = array_slice($array1, 0, -1);

    for ($j = $i + 1; $j < $length; $j++)
    {
        if (!array_key_exists($j, $array))
        {
            continue;
        }

        $array2 = $array[$j];
        $values2 = array_slice($array2, 0, -1);

        if ($values1 == $values2)
        {
            $array1[6] += $array2[6];

            unset($array[$j]);
        }
    }

    $resultArray[] = $array1;
}

var_dump($resultArray);

or

$resultArray = [];

for ($i = 0; $i < count($array); $i++)
{
    $array1 = $array[$i];
    $values1 = array_slice($array1, 0, -1);

    $length = count($array);

    for ($j = $i + 1; $j < $length; $j++)
    {
        if (!array_key_exists($j, $array))
        {
            continue;
        }

        $array2 = $array[$j];
        $values2 = array_slice($array2, 0, -1);

        if ($values1 == $values2)
        {
            $array1[6] += $array2[6];

            unset($array[$j]);
        }
    }

    $array = array_values($array);

    $resultArray[] = $array1;
}

var_dump($resultArray);

or

$resultArray = [];

for ($i = 0; $i < count($array); $i++)
{
    $array1 = $array[$i];
    $values1 = array_slice($array1, 0, -1);

    for ($j = $i + 1; $j < count($array); $j++)
    {
        $array2 = $array[$j];
        $values2 = array_slice($array2, 0, -1);

        if ($values1 == $values2)
        {
            $array1[6] += $array2[6];

            unset($array[$j]);

            $j--;
            $array = array_values($array);
        }
    }

    $resultArray[] = $array1;
}

var_dump($resultArray);

First solution should be the fastest

I also checked that isset($array[$i]) works much faster than arary_key_exists($i, $array)

6 Comments

This is a step in the right direction, however your code does not completely work, if there are 6 duplicate arrays all with [6] = 1, then it creates 2 arrays one of [6] = 4 and one of [6] = 2. Like its not fully looping trough them.
@Dr.Banana thanks for testing, i think i did it without errors. Test it plzz again :)
I tested it again, both 2 and 3 work properly, the first however is inconsistent. After testing the speed, I noticed that 3 is faster on my setup, so I went with 3. Thanks for your help!
@Dr.Banana what do you mean 'inconsistent' ? about speed ?
The result it outputs, changes with the same data only in a diffrent order, where both 2nd and the 3th work as expected.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.