doormen2014 2018-08-24 23:36
浏览 7
已采纳

PHP采样的值相同

I have an array with data:

array(4) {
    [0]=>
        array(3) {
            ["number"]=>
            string(10) "8678664470"
            ["created_at"]=>
            string(10) "2018-08-11"
            ["title"]=>
            string(18) "Quaerat voluptatem"
        }
    [1]=>
         array(3) {
             ["number"]=>
             string(10) "8678664470"
             ["created_at"]=>
             string(10) "2018-08-11"
             ["title"]=>
             string(15) "Et dolore magna"
        }
    [2]=>
        array(3) {
            ["number"]=>
            string(10) "0535783461"
            ["created_at"]=>
            string(10) "2018-08-15"
            ["title"]=>
            string(27) "Laborious physical exercise"
        }
    [3]=>
        array(3) {
            ["number"]=>
            string(10) "0535783461"
            ["created_at"]=>
            string(10) "2018-08-15"
            ["title"]=>
            string(15) "Natus error sit"
        }
}

How can I make this array? Need to collect all title, where the same number and created_at.

array(2) {
  [0]=>
    array(3) {
        ["number"]=>
        string(10) "8678664470"
        ["created_at"]=>
        string(10) "2018-08-11"
        ["title"]=>
            array(2) {
                [0]=>
                    string(18) "Quaerat voluptatem"
                [1]=>
                    string(15) "Et dolore magna"
            }
     }
   [1]=>
     array(3) {
         ["number"]=>
         string(10) "0535783461"
         ["created_at"]=>
         string(10) "2018-08-15"
         ["title"]=>
            array(2) {
                [0]=>
                    string(27) "Laborious physical exercise"
                [1]=>
                    string(15) "Natus error sit"
            }
    }
 }
  • 写回答

1条回答 默认 最新

  • dongxia9620 2018-08-25 00:08
    关注

    Here's an approach using an associative array to accumulate titles for entries that have been seen. This does it in one pass (linear time complexity):

    function mergeTitles($data) {
        $result = [];
    
        foreach ($data as $e) {
            $key = $e['number'] . "&" . $e['created_at'];
    
            if (!array_key_exists($key, $result)) {
                $result[$key] = $e;
                $result[$key]['title'] = [];
            }
    
            $result[$key]['title'][] = $e['title'];
        }
    
        return array_values($result);
    }
    

    Output:

    array(2) {
      [0]=>
      array(3) {
        ["number"]=>
        string(10) "8678664470"
        ["created_at"]=>
        string(10) "2018-08-11"
        ["title"]=>
        array(2) {
          [0]=>
          string(18) "Quaerat voluptatem"
          [1]=>
          string(15) "Et dolore magna"
        }
      }
      [1]=>
      array(3) {
        ["number"]=>
        string(10) "0535783461"
        ["created_at"]=>
        string(10) "2018-08-15"
        ["title"]=>
        array(2) {
          [0]=>
          string(27) "Laborious physical exercise"
          [1]=>
          string(15) "Natus error sit"
        }
      }
    }
    

    Test at this repl.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了