My multi-dimensional array code is as follows: $ arrarray (array (id & gt; 7363, fd & gt; 1, fb & gt; 1, tcp & gt; 0, fbdh & gt; 1, jxs & gt; GZYX), array (id & gt; 7412, fd & gt; 1, fb & gt; my multi-dimensional array code is as follows:
$ Arr = array ('id' => 7363, 'fd '=> 1, 'FB' => 1, 'tcp '=> 0, 'fbdh '=> 1, 'jxs' => 'gzyx'), array ('id' => 7412, 'fd '=> 1, 'FB' => 0, 'tcp '=> 1, 'fbdh' => 1, 'jxs '=> 'gzyx'), array ('id' => 7512, 'fd' => 1, 'FB '=> 1, 'tcp' => 0, 'fbdh '=> 1, 'jxs' => 'gzyx'), array ('id' => 7516, 'fd '=> 1, 'FB' => 0, 'tcp '=> 1, 'fbdh' => 0, 'jxs '=> 'bjcj '),); // It means that the jxs in the array are the same as their fd, fb, tcp, and fbdh. // How can I convert the above array into the following result? // result: GZYX 3 2 1 3 BJCJ 1 0 1 0
---------- Solution ------------
7363,'fd'=>1,'fb'=>1,'tcp'=>0,'fbdh'=>1,'jxs'=>'GZYX'), array('id'=>7412,'fd'=>1,'fb'=>0,'tcp'=>1,'fbdh'=>1,'jxs'=>'GZYX'), array('id'=>7512,'fd'=>1,'fb'=>1,'tcp'=>0,'fbdh'=>1,'jxs'=>'GZYX'), array('id'=>7516,'fd'=>1,'fb'=>0,'tcp'=>1,'fbdh'=>0,'jxs'=>'BJCJ'), ); $result = []; foreach ($arr as $a) { if (empty($result[$a['jxs']])) { $result[$a['jxs']] = $a; } else { foreach ($a as $k => $v) { $result[$a['jxs']][$k] += $v; } } } foreach ($result as $k => $r) { echo sprintf("%s %d %d %d %d\n", $k, $r['fd'], $r['fb'], $r['tcp'], $r['fbdh']); }