Monday, 27 August 2018

How do you get unique values ​​in a multidimensional array and add values ​​by single value?

I have an array.
array(6) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(4)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(5)
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(7) "Aprelle"
    ["collect"]=>
    int(2)
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(4) "Just"
    ["collect"]=>
    int(3)
  }
  [4]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(3)
  }
  [5]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(2)
  }

I want to get the every unique name and add every instance of collect value per unique name.
Output:
array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(9)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(7) "Aprelle"
    ["collect"]=>
    int(2)
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(6) "Just"
    ["collect"]=>
    int(3)
  }
  [4]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(5)
  }

So far, i can only get the names correctly. how can I add the collect value per name?
This is my code
    $stat = array ( 0 => array ( 'name' => 'Sarah', 'collect' => 4, ), 1 => array ( 'name' => 'Sarah', 'collect' => 5, ), 2 => array ( 'name' => 'Aprelle', 'collect' => 2, ), 3 => array ( 'name' => 'Just', 'collect' => 3, ), 4 => array ( 'name' => 'Sarah', 'collect' => 3, ), 5 => array ( 'name' => 'Sarah', 'collect' => 2, ), );

$datasetCount = count($stat); // returns count of array items of any array
echo "<h1>There are $datasetCount names</h1>";
$pname = "";
$pcollect = 0;

$i = 0;
foreach ($stat as $each_name) {
    $i++;
    $storename = "";
    echo "<h2>Name $i</h2>"; 

    while (list($key, $value) = each ($each_name)) { 

    if($i == 1)
    {
        if($key == 'name')
        {
            $pname = $value;
            echo "<br/> Name: ".$pname."<br/>";
        }
        if($key == 'collect')
        {
            $pcollect = $value;
            echo "<br/> Collect: ".$pcollect."<br/>";
        }
    }
    else
    {
        if($key == 'name')
        {
            if($pname != $value)
            {
                $pname = $value;
                $storename = $value;
                $check = 1;
                echo "<br/>".$storename."<br/>";
            }else
            {
                $check = 0;
                echo "<br/>".$storename."<br/>";
            }
        } 

    }       

}
}

Output should be in this order:
Name:Sarah
Collect: 9

Aprelle
Collect: 2

Just
Collect: 3

Sarah
Collect: 5

Check if the next name is the same with the first name, if yes, merge them. and check again if the next is the same.

I would do it as follows:
$newArr = array();

foreach($stat as $val) {
    if(isset($newArr[$val['name']])) {
        $newArr[$val['name']] += $val['collect'];
    } else {
        $newArr[$val['name']] = $val['collect'];
    }
}

and if you really need the format
array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(9)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(7) "Aprelle"
    ["collect"]=>
    int(2)
  }
}

then add:
$newArr2 = array();

foreach($newArr as $key=>$val) {
    $newArr2[] = array('name' => $key, 'collect' => $val);
}

EDIT:
Version that will add "collect" only if the current "name" is the same as the previous "name".
$newArr = array();

for($i = 0; $i<count($stat); $i++) {
    // check if $newArr has any values, it will be false only the first loop
    if(isset($newArr[key($newArr)]['name'])) {
        // check if the current value of the loop is the same as the last value in the new array
        if($stat[$i]['name'] == $newArr[key($newArr)]['name']) {
            // if it is, this means that we just have to add the current "collect" value to the last element of $newArr
            $newArr[key($newArr)]['collect'] += $stat[$i]['collect'];
        } else {
            // if not, we have to create a new value
            $newArr[] = array('name' => $stat[$i]['name'], 'collect' => $stat[$i]['collect']);
            // we have to go to the newly added value of the new array
            next($newArr);
        }
    } else {
        $newArr[] = array('name' => $stat[$i]['name'], 'collect' => $stat[$i]['collect']);
    }
}

0 comments:

Post a Comment