3 - 维数组插入PHP

时间:2017-04-12 19:24:12

标签: php arrays multidimensional-array

我有一个PHP数组

$res = array(

    0 =>array(
      'task' => 'Call with Training',
      'instruction' => 'ins',
      'color' =>  '#f48718',
      'legend' =>  'Home Study'),

     1 =>array(
      'task' => 'Culture & Values',
      'instruction' => 'ins',
      'color' =>  '#f48718',
      'legend' =>  'Home Study'),

     2 =>array(
     'task' => 'Call with Training',
      'instruction' => 'ins',
      'color' =>  '#f48718',
      'legend' =>  'Home Study'),

     3 =>array(
      'task' => 'Call with Training',
      'instruction' => 'ins and training',
      'color' =>  '#f48718',
      'legend' =>  'Home Study')
);

我正在尝试计算重复的密钥并将它们组合在一起并尝试得到如下结果:

$upd = array(
    'Call with Training' => array(
        0 => array(
              'instruction' => 'ins',
              'color' =>  '#f48718',
              'legend' =>  'Home Study',
              'count' => 2
        ),
        1 => array(
              'instruction' => 'ins and training',
              'color' =>  '#f48718',
              'legend' =>  'Home Study',
              'count' => 1
        )
    ),
    'Culture & Values' => array(
        0 => array(
              'instruction' => 'ins',
              'color' =>  '#f48718',
              'legend' =>  'Home Study',
              'count' => 1
        )
    )
);

我的代码到现在为止:

foreach($res as $s){
    $key = $s['task'];

    if(isset($upd[$key])) {
        foreach($upd[$key] as $x){
            if($x['task'] === $s['task'] && $x['instruction'] === $s['instruction']){
                $x['count']++;
            }
            else{
                $upd[$key][] = array('task' => $s['task'], 'instruction' => $s['instruction'], 'count' => 1,'color' =>  $s['color'], 'legend' => $s['legend']);
            }
        }
    }
    else{
        $upd[$key][] = array('task' => $s['task'], 'instruction' => $s['instruction'], 'count' => 1,'color' =>  $s['color'], 'legend' => $s['legend']);
    }

}

如果有人能帮助我这样做会很棒。谢谢。

1 个答案:

答案 0 :(得分:0)

这里:

// source array
$res = array(

    0 =>array(
      'task' => 'Call with Training',
      'instruction' => 'ins',
      'color' =>  '#f48718',
      'legend' =>  'Home Study'),

     1 =>array(
      'task' => 'Culture & Values',
      'instruction' => 'ins',
      'color' =>  '#f48718',
      'legend' =>  'Home Study'),

     2 =>array(
     'task' => 'Call with Training',
      'instruction' => 'ins',
      'color' =>  '#f48718',
      'legend' =>  'Home Study'),

     3 =>array(
      'task' => 'Call with Training',
      'instruction' => 'ins and training',
      'color' =>  '#f48718',
      'legend' =>  'Home Study')
);
$new = [];
foreach ($res as $v) {
    $task = $v['task'];

    if (empty($new[$task])) {
        $new[$task] = [];
    }

    $ins = $v['instruction'];
    if (!isset($new[$task][$ins])) {
        $v['count'] = 1;
        $new[$task][$ins] = $v;
    } else {
        $new[$task][$ins]['count']++;
    }
}

echo'<pre>Before: ',print_r($new),'</pre>';

// to make keys as numbers - use `array_values`
$new = array_map('array_values', $new);

echo'<pre>After: ',print_r($new),'</pre>';