码迷,mamicode.com
首页 > 其他好文 > 详细

Laravel collect妙用

时间:2018-09-06 02:33:37      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:php   highlight   word   UNC   comment   function   ram   any   oar   

需求

     $arr = array(
         [
             ‘name‘     => "shawn",
             "email"    => "shawn@qq.com",
             "company"  => "QQ"
         ],
         [
             ‘name‘     => "nicole",
             "email"    => "nicole@360.com",
             "company"  => "360"
         ],
         [
             ‘name‘     =>  "test",
             "email"    => "test@baidu.com",
             "company"  => "baidu"
         ]
     );
//转化成
     $lookup = array(
         "shawn"    =>  "shawn@qq.com",
         "nicole"   => "nicole@360.com",
         "test"     =>"test@baidu.com"
     );

方法一:

$lookup = collect($arr)->pluck("email","name")->toarray();

方法二:

$lookup  =  collect($arr)->reduce(function($lookup,$item){
            $lookup[$item["name"]] = $item["email"];
            return $lookup;
    },[]);


//reduce方法调用的是 array_reduce()方法
//array_reduce($arr,callback,initial);

Laravel collect妙用

标签:php   highlight   word   UNC   comment   function   ram   any   oar   

原文地址:https://www.cnblogs.com/qaing123/p/9595410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!