怎么在PHP中合并具有相同id值的数组元素

在PHP中有以下数组.Id和Total键对于相同的Id值是相同的.数组如下: Array( [0] = Array ( [id] = 1 [total] = 100 [line] = books [value] = 10 ) [1] = Array ( [id] = 1 [total] = 100 [line] = cars [value] = 50 ) [2] = Array ( [id] = 1 [total] = 100 [line] = cover [value] = 30 ) [3] = Arr
 
在PHP中有以下数组.Id和Total键对于相同的Id值是相同的.数组如下:
Array
(
    [0] => Array
        (
            [id]    => 1
            [total] => 100
            [line]  => books
            [value] => 10
        )
    [1] => Array
        (
            [id]    => 1
            [total] => 100
            [line]  => cars
            [value] => 50
        )
    [2] => Array
        (
            [id]    => 1
            [total] => 100
            [line]  => cover
            [value] => 30
        )
    [3] => Array
        (
            [id]    => 2
            [total] => 80
            [line]  => books
            [value] => 5
        )
    [4] => Array
        (
            [id]    => 2
            [total] => 80
            [line]  => cover
            [value] => 35
        )
    [5] => Array
        (
            [id]    => 2
            [total] => 80
            [line]  => cars
            [value] => 40
        )
想在一个数组中合并Line和Value元素.下面是想实现的效果:
Array
(
    [0] => Array
        (
            [id]      => 1
            [total]   => 100
            [details] => Array
               (
                   [0] => Array
                      (
                         [line]  => books
                         [value] => 10
                      )
                   [1] => Array
                      (
                         [line]  => cars
                         [value] => 50
                      )
                   [2] => Array
                      (
                         [line]  => cover
                         [value] => 30
                      )
               ) 
        )                      

    )
    [1] => Array
        (
            [id]      => 2
            [total]   => 80
            [details] => Array
               (
                   [0] => Array
                      (
                         [line]  => books
                         [value] => 5
                      )
                   [1] => Array
                      (
                         [line]  => cars
                         [value] => 35
                      )
                   [2] => Array
                      (
                         [line]  => cover
                         [value] => 40
                      )
               ) 
        )                      
    )
实现代码如下:
$out=array();
foreach($array as $x){
  $out[$x['id']]['id']=$x['id'];
  $out[$x['id']]['total']=$x['total'];
  $out[$x['id']]['details'][]=array('line'=>$x['line'],'value'=>$x['value']);
}

笔记:
使用id作为out键,可以添加我们自己的,但这将工作正常.
id和total将在每个循环(每个键)上被覆盖,但这没什么大不了的
line\value递增到自己的details数组
 
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

关键词匹配是比较常见的需求,如留言、弹幕及游戏聊天中的敏感词过滤,都需要对一段文字进行关键词匹配。提取到关键词后,再做进一步处理。 本类借助PHP高效的数组和mbstring扩展,来实现对中文关键词的匹配。主要思想是以关键词为key,构
本文实例讲述了php中最简单的字符串匹配算法。分享给大家供大家参考。具体实现方法如下: ?php/*最简单字符串匹配算法php实现方式 T: ababcabcP: abc 0. 1. 2.ababcabc ababcabc ababcabc||| ||| |||abc abc abc(X) (X) (O) 3. 4. 5.ababcabc ababcabc ababcabc ||| ||| ||| abc abc abc
Ajax跨域请求PHP header设置,具体实例代码如下: header('Access-Control-Allow-Origin : *');header('Access-Control-Allow-Headers : Origin, X-Requested-With, Content-Type, Accept');header('Access-Control-Allow-Methods :PUT,POST,GET,DELETE,OPTIONS');//持久化sessionheader("Access
虽然php中的header()函数 下载文件不支持断点续传功能但有时我们还真需要此功能,如我们下载txt,图片文件时如果直接是个连接估计是直接打开了而不是下载了,那么我们可如何实现下载呢。 /*** 文件下载**/header("Content-type:text/html;charset=utf-8");d
PHP将HTML页面输出为word文档,具体实例代码如下: $html = include ('html文件');echo $html; ob_start(); //打开缓冲区 header("Cache-Control: public"); Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); $riqi=date("Ymd"); if (strpos($_SERVER["HTTP_USER_A
phpword读取内容和样式 生成新的内容,目前有 rows cell textrun等样式,顺序不固定 可以设定,具体实例代码如下: ?phprequire 'vendor/autoload.php';use PhpOffice\PhpWord\PhpWord;use PhpOffice\PhpWord\IOFactory;use PhpOffice\PhpWord\Style\Font;use PhpOffice\PhpWord\Shared\ZipArchive;use