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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效