Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
三维数组PHP插入_Php_Arrays_Multidimensional Array - Fatal编程技术网

三维数组PHP插入

三维数组PHP插入,php,arrays,multidimensional-array,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',

我有一个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']);
    }

}
如果有人能过来帮我做这件事,那就太好了。谢谢。

这里:

// 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>';
//源数组
$res=数组(
0=>数组(
“任务”=>“通过培训呼叫”,
“指令”=>“ins”,
“颜色”=>“#f48718”,
“图例”=>“家庭学习”),
1=>数组(
“任务”=>“文化与价值观”,
“指令”=>“ins”,
“颜色”=>“#f48718”,
“图例”=>“家庭学习”),
2=>数组(
“任务”=>“通过培训呼叫”,
“指令”=>“ins”,
“颜色”=>“#f48718”,
“图例”=>“家庭学习”),
3=>数组(
“任务”=>“通过培训呼叫”,
“说明”=>“ins和培训”,
“颜色”=>“#f48718”,
“图例”=>“家庭学习”)
);
$new=[];
foreach($res作为$v){
$task=$v['task'];
if(空($new[$task])){
$new[$task]=[];
}
$ins=$v[“指令”];
如果(!isset($new[$task][$ins])){
$v['count']=1;
$new[$task][$ins]=$v;
}否则{
$new[$task][$ins]['count']++;
}
}
echo'Before:',print_r($new),'';
//要使键成为数字,请使用“数组”值`
$new=数组映射('array\u values',$new);
回音“后:”,打印($new),“”;

哪些键被认为是重复的?@u\u mulder如果任务名称相同,则应增加计数。所以任务名称是关键。@u\u mulder如果您看到第二个代码块,您可以看到任务名称“通过培训调用”有两个条目。因为它们有不同的“指令”。顺便说一句,数组中不能有相同的键,所以您必须编辑预期的结果。@u\u mulder我已经编辑过了。@u\u mulder您知道如何在angular js中迭代这个数组吗。再次谢谢。我对angular不熟悉,所以不,我不知道。