如何在多维数组中获取唯一值并为每个唯一值添加值?

时间:2015-05-24 11:15:21

标签: php arrays multidimensional-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)
  }

我想获取每个唯一名称,并按唯一名称添加每个collect值的实例。

输出:

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)
  }

到目前为止,我只能正确地获取名称。如何为每个名称添加收集值?

这是我的代码

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

    }       

}
}

输出应按此顺序排列:

Name:Sarah 
Collect: 9

Aprelle 
Collect: 2

Just 
Collect: 3

Sarah
Collect: 5

检查下一个名称是否与第一个名称相同,如果是,则合并它们。并再次检查下一个是否相同。

4 个答案:

答案 0 :(得分:2)

我会这样做:

$newArr = array();

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

如果你真的需要格式

array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "Sarah"
    ["collect"]=>
    int(9)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(7) "Aprelle"
    ["collect"]=>
    int(2)
  }
}

然后添加:

$newArr2 = array();

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

修改

将添加&#34;收集&#34;的版本只有当前&#34;名称&#34;与之前的&#34;名称&#34;。

相同
$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']);
    }
}

答案 1 :(得分:0)

在输出数据之前准备数据会容易得多。您可以使用以下命令创建包含相关值的新数组。

$totals = [];
foreach($stat as $entry){
    if(!isset($totals[$entry['name']])){
        $totals[$entry['name']] = $entry['collect'];
    }else{
        $totals[$entry['name']] += $entry['collect'];
    }   
}

var_dump($totals);

没有按名称编制索引的收集意味着,如果您想要按名称显示它,则必须再次针对您想要的每个名称迭代整个列表秀。

编辑:您首选的输出让我失望。根据您的期望更新了代码

答案 2 :(得分:0)

array.last

答案 3 :(得分:0)

创建一个由名称索引的二维数组并收集值:

$unique = array();
foreach ($stats as $s) {
    if (isset($unique[$s['name']]) {
        $unique[$s['name']][$s['collect']] = $s;
    } else {
        $unique[$s['name']] = array($s['collect'] => $s);
    }
}

然后你可以完成这个步骤,收集同一级别的所有值:

$result = array();
foreach ($unique as $by_name) {
    $result = array_merge($result, $by_name);
}