laravel Êý×éÒƳýÏàͬ
laravel ÊÇÒ»¿î¹ãÊܽӴýµÄ php ¿ª·¢¿ò¼Ü£¬ÌṩÁËÐí¶àÀû±ãµÄ¸¨Öúº¯Êý¡£ÔÚ¿ª·¢Àú³ÌÖУ¬¾³£»áÓÐÐèÒªÒƳýÊý×éÖÐÏàֵͬµÄÇéÐΡ£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓà laravel ×Ô´øµÄ¸¨Öúº¯ÊýÀ´ÊµÏÖÕâ¸ö¹¦Ð§¡£
ʹÓà array_unique º¯Êý
array_unique º¯ÊýÊÇ PHP ×Ô´øµÄÒ»¸öº¯Êý£¬¿ÉÒÔÓÃÀ´ÒƳýÊý×éÖеÄÖظ´Öµ¡£Ê¹ÓÃÕâ¸öº¯ÊýºÜÊǼòÆÓ£¬Ö»ÐèÒª½«ÐèҪȥÖصÄÊý×é×÷Ϊ²ÎÊý´«Èë¼´¿É¡£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÊý×é $arr£¬ËüµÄÔªËØÈçÏ£º
$arr = [1, 2, 2, 3, 4, 4, 4, 5];
µÇ¼ºó¸´ÖÆ
ÈôÊÇÒªÒƳýÆäÖеÄÖظ´ÔªËØ£¬¿ÉÒÔʹÓà array_unique º¯Êý£º
$uniqueArr = array_unique($arr);
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬$uniqueArr ¾ÍÊÇÒ»¸öÈ¥ÖغóµÄÊý×é¡£
¿ÉÊÇÕâÖÖÒªÁìÓÐÒ»¸öÈõµã£¬¼´ÒƳýºóµÄÊý×éÔªËØϱê»á±»ÖØÖã¬Òò´Ë²»ÊÊÓÃÓÚÐèÒª±£´æÔÓÐϱêµÄÇéÐΡ£
ʹÓà Laravel µÄ¸¨Öúº¯Êý
Laravel ÌṩÁËһϵÁÐÀû±ãÊÊÓõĸ¨Öúº¯Êý£¬ÆäÖоͰüÀ¨ÓÃÓÚ´¦Öóͷ£Êý×éµÄº¯Êý¡£ÆäÖÐÒ»¸ö³£Óõĺ¯ÊýÊÇ collect º¯Êý£¬¿ÉÒÔ½«Ò»¸öÊý×é°üװΪÜöÝÍ£¬È»ºóʹÓÃÜöÝÍÌṩµÄÒªÁìÀ´¾ÙÐвÙ×÷¡£
ʹÓà collect º¯Êý½«Êý×é·âװΪÜöÝÍ£º
$collection = collect($arr);
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
½Ó×Å£¬ÎÒÃÇ¿ÉÒÔʹÓà unique ÒªÁìÀ´ÒƳýÏàͬԪËØ£º
$uniqueCollection = $collection->unique();
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÕâÑù£¬$uniqueCollection ¾ÍÊÇÒ»¸öÈ¥ÖغóµÄÜöÝÍ¡£
Óë array_unique º¯Êý²î±ðµÄÊÇ£¬Ê¹Óà unique ÒªÁìºó£¬±£´æÁËÔÓÐÊý×éÔªËصÄϱꡣÈôÊÇÐèÒª·µ»Ø²»´øϱêµÄÊý×飬¿ÉÒÔʹÓà values ÒªÁ죺
$uniqueArr = $uniqueCollection->values()->all();
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬$uniqueArr ¾ÍÊÇÒ»¸öÈ¥Öغ󣬲»´øÓÐϱêµÄÊý×é¡£
ʹÓà Laravel µÄ Collection Àà
³ýÁËʹÓø¨Öúº¯Êý£¬Laravel »¹ÌṩÁËÁíÍâÒ»ÖÖÔ½·¢Àû±ãµÄÒªÁ죬×ÝÈ»Óà Collection Àà¡£
Collection ÀàÊÇ Laravel ÌṩµÄÒ»¸öÜöÝÍÀ࣬·â×°Á˶ÔÊý×éµÄÖݪֲÙ×÷¡£Ê¹Óà Collection Àà¿ÉÒÔÔ½·¢Àû±ãµØ¾ÙÐÐÊý×é²Ù×÷¡£
ʹÓà Collection Àà¾ÙÐÐÈ¥ÖزÙ×÷µÄ°ì·¨ÈçÏ£º
Ê×ÏÈ£¬½¨ÉèÒ»¸ö Collection ʵÀý£º
$collection = collect($arr);
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
È»ºó£¬Ê¹Óà unique ÒªÁì¾ÙÐÐÈ¥ÖØ£º
$uniqueCollection = $collection->unique();
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
×îºó£¬¿ÉÒÔʹÓà toArray ÒªÁ콫ÜöÝÍת»»ÎªÊý×飺
$uniqueArr = $uniqueCollection->toArray();
µÇ¼ºó¸´ÖÆ
ÓëʹÓø¨Öúº¯ÊýµÄ·½·¨Ïà±È£¬Ê¹Óà Collection Àà¿ÉÒÔïÔÌ´úÂëÈßÓ࣬ʹ´úÂëÔ½·¢¼òÆÓÒ׶®¡£
×ܽá
±¾ÎÄÏÈÈÝÁËʹÓà Laravel ʵÏÖÒƳýÊý×éÖÐÏàֵͬµÄÒªÁ졣ʹÓà PHP ×Ô´øµÄ array_unique º¯Êý¿ÉÒÔºÜÀû±ãµØÈ¥ÖØÊý×飬µ«ÓпÉÄܵ¼ÖÂÊý×éϱêÖØÖõÄÎÊÌâ¡£¶øʹÓà Laravel µÄ¸¨Öúº¯Êý»ò Collection Àà¿ÉÒÔ×èÖ¹Õâ¸öÎÊÌ⣬²¢ÇÒ¿ÉÒÔÌṩԽ·¢Àû±ãµÄ´úÂëʵÏÖ·½·¨¡£ÔÚÏÖʵ¿ª·¢ÖУ¬¿ÉÒÔƾ֤ÐèÇóÑ¡ÔñÊʺϵÄÒªÁìÀ´¾ÙÐÐÊý×éÈ¥ÖزÙ×÷¡£
ÒÔÉϾÍÊÇlaravel Êý×éÒƳýÏàͬµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡