V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jinganchuqi
V2EX  ›  PHP

请问 php 中怎样合并二维数组相同的 key?

  •  1
     
  •   jinganchuqi · 2015-04-02 18:33:23 +08:00 · 4463 次点击
    这是一个创建于 3305 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问怎样合并period相同,并且price相同的数组?

    合并前
    $arr = array(
    array("id"=>1,'num'=>5,'period'=>3,'price'=>5),
    array("id"=>2,'num'=>10,'period'=>3,'price'=>5),
    array("id"=>3,'num'=>15,'period'=>9,'price'=>20),
    );

    合并后
    $arr = array(
    array('num'=>15,'period'=>3,'price'=>5),
    array('num'=>15,'period'=>9,'price'=>20),
    );
    第一次发贴,多多见谅哈!

    20 条回复    2015-04-03 17:22:41 +08:00
    kmvan
        1
    kmvan  
       2015-04-02 19:12:56 +08:00   ❤️ 1
    <?php

    $old = [
    ["id"=>1,'num'=>5,'period'=>3,'price'=>5],
    ["id"=>2,'num'=>10,'period'=>3,'price'=>5],
    ["id"=>3,'num'=>15,'period'=>9,'price'=>20]
    ];

    $new = [];
    foreach($old as $k => $v){
    if(isset($new[$v['period']])){
    $new[$v['period']]['num'] += $v['num'];
    }else{
    unset($v['id']);
    $new[$v['period']] = $v;
    }
    }

    var_dump($new);


    ?>
    输出耗时:0.00074601173400879ms
    tuoxie007
        2
    tuoxie007  
       2015-04-02 19:23:23 +08:00
    就三条数据,测耗时意义不大吧。。。
    tuoxie007
        3
    tuoxie007  
       2015-04-02 19:23:37 +08:00
    @kmvan 就三条数据,测耗时意义不大吧。。。
    kmvan
        4
    kmvan  
       2015-04-02 19:37:03 +08:00
    @tuoxie007 就三条数据,测耗时意义不大吧。。。
    你一定是妒忌我和我的头像
    jinganchuqi
        5
    jinganchuqi  
    OP
       2015-04-02 19:40:12 +08:00
    @kmvan 谢谢,但是我要的是 合并period相同,并且price也相同的数组呀,您还能给我说说其他方法吗?
    kmvan
        6
    kmvan  
       2015-04-02 19:56:37 +08:00
    @jinganchuqi 我要的是 合并period相同,并且price也相同的数组呀,您还能给我说说其他方法吗?
    你的结果 price 也没有相同。
    kmvan
        7
    kmvan  
       2015-04-02 19:59:19 +08:00   ❤️ 1
    <?php

    $old = [
    ["id"=>1,'num'=>5,'period'=>3,'price'=>5],
    ["id"=>2,'num'=>10,'period'=>3,'price'=>5],
    ["id"=>3,'num'=>15,'period'=>9,'price'=>20]
    ];

    $new = [];
    foreach($old as $k => $v){
    if(isset($new[$v['period']])){
    $new[$v['period'] . $v['price']]['num'] += $v['num'];
    }else{
    unset($v['id']);
    $new[$v['period'] . $v['price']] = $v;
    }
    }

    var_dump($new);

    //这样就ok了啊。isset的效率略高。
    jinganchuqi
        8
    jinganchuqi  
    OP
       2015-04-02 20:02:52 +08:00
    @kmvan 好吧,对不起,是我表达不清楚 。 我的意思是 以period 和 price 都相同 来作为合并的条件。
    jinganchuqi
        9
    jinganchuqi  
    OP
       2015-04-02 20:05:30 +08:00
    @kmvan 十分感谢您啦!!!!
    l1905
        10
    l1905  
       2015-04-02 20:20:51 +08:00   ❤️ 1
    <?php
    $arr = array(
    array("id"=>1,'num'=>5,'period'=>3,'price'=>5),
    array("id"=>2,'num'=>10,'period'=>3,'price'=>5),
    array("id"=>3,'num'=>15,'period'=>9,'price'=>20),
    );
    $result = array();
    $check = array();
    foreach ($arr as $k => $data) {
    $ser = serialize(array('period'=>$data['period'], 'price'=>$data['price']));
    if(!in_array($ser, $check)) {
    array_push($check, $ser);
    $result[$ser] = array('num'=>$data['num'],'period'=>$data['period'], 'price'=>$data['price']);
    } else {
    $result[$ser]['num'] += $data['num'];
    }
    }
    var_dump(array_values($result));

    ?>
    实现效率不肿么高。。。
    kn007
        11
    kn007  
       2015-04-03 00:17:59 +08:00
    学习一下
    mhycy
        12
    mhycy  
       2015-04-03 01:01:27 +08:00
    mhycy
        13
    mhycy  
       2015-04-03 01:02:51 +08:00
    @kmvan 之前那个写法不怕period和price连在一起出问题么?都是数字。。。
    kmvan
        14
    kmvan  
       2015-04-03 01:17:57 +08:00 via Android
    @mhycy 之前那个写法不怕period和price连在一起出问题么?都是数字。。。
    显然不会。而且最高效,跑10w次1ms都不用
    Sinute
        15
    Sinute  
       2015-04-03 01:27:07 +08:00
    https://gist.github.com/Sinute/f67544f3bbcabe8d126c

    @kmvan
    试试 period=11, price=12和period=1, price=112
    mhycy
        16
    mhycy  
       2015-04-03 01:29:21 +08:00
    mhycy
        17
    mhycy  
       2015-04-03 01:35:11 +08:00
    @Sinute
    好高端的写法。。。
    else没有花括号不能忍。。
    0x000
        18
    0x000  
       2015-04-03 10:01:19 +08:00
    array_unique(array_merage(array_column($arr, 'period'), array_column($arr, 'price')));

    或者使用array_map,或者使用傻瓜式循环
    kmvan
        19
    kmvan  
       2015-04-03 10:10:32 +08:00
    我漏了一个地方
    if(isset($new[$v['period']])){
    改成
    if(isset($new[$v['period'] . $v['price']])){
    msg7086
        20
    msg7086  
       2015-04-03 17:22:41 +08:00
    @kmvan 最好把key单独赋值,不容易出错。

    https://gist.github.com/msg7086/01761d72e1d8c2cf12ae
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   901 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:25 · PVG 05:25 · LAX 14:25 · JFK 17:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.