本文来自GitHub开源项目点我跳转30秒PHP代码片段精选有用的PHP代码片段合集,30秒以内看懂这些碎片。排列所有如果提供的函数返回true的数字等于数组中成员数的总和,则函数返回true,否则返回false。functionall($items,$func){returncount(array_filter($items,$func))===count($items);}Examplesall([2,3,4,5],function($item){返回$item>1;});//如果提供的函数对数组中的至少一个元素返回true,则trueany返回true,否则返回false。函数any($items,$func){returncount(array_filter($items,$func))>0;}Examplesany([1,2,3,4],function($item){return$item<2;});//truedeepFlatten(deepflattenedarray)将多维数组转换为一维数组functiondeepFlatten($items){$result=[];foreach($itemsas$item){if(!is_array($item)){$result[]=$item;}else{$result=array_merge($result,deepFlatten($item));}}return$result;}ExamplesdeepFlatten([1,[2],[[3],4],5]);//[1,2,3,4,5]drop返回一个新数组,其中n个元素从左侧弹出。函数drop($items,$n=1){returnarray_slice($items,$n);}Examplesdrop([1,2,3]);//[2,3]drop([1,2,3],2);//[3]findLast返回提供的函数为其返回有效值(即过滤后的值)的最后一个元素的键值(value)。函数findLast($items,$func){$filteredItems=array_filter($items,$func);returnarray_pop($filteredItems);}ExamplesfindLast([1,2,3,4],function($n){return($n%2)===1;});//3findLastIndex返回键名(key)提供的函数为其返回有效值(即过滤值)的最后一个元素。函数findLastIndex($items,$func){$keys=array_keys(array_filter($items,$func));returnarray_pop($keys);}ExamplesfindLastIndex([1,2,3,4],function($n){return($n%2)===1;});//2flatten(flatarray)减少了数组到一维数组functionflatten($items){$result=[];foreach($itemsas$item){if(!is_array($item)){$result[]=$item;}else{$result=array_merge($result,array_values($item));}}return$result;}Examplesflatten([1,[2],3,4]);//[1,2,3,4]groupBy根据给定的函数对数组的元素进行分组。函数groupBy($items,$func){$group=[];foreach($itemsas$item){if((!is_string($func)&&is_callable($func))||function_exists($func)){$key=call_user_func($func,$item);}$group[$key][]=$item;}elseif(is_object($item)){$group[$item->{$func}][]=$item;}elseif(isset($item[$func])){$group[$item[$func]][]=$item;}}return$group;}ExamplesgroupBy(['一','二','三'],'strlen');//[3=>['one','two'],5=>['three']]hasDuplicates(重复检查)检查数组中的重复值。如果存在重复值则返回true,如果所有值都是唯一的则返回false。functionhasDuplicates($items){returncount($items)>count(array_unique($items));}ExampleshasDuplicates([1,2,3,4,5,5]);//truehead返回数组元素中的第一项。functionhead($items){returnreset($items);}Exampleshead([1,2,3]);//1last返回数组中的最后一个元素。functionlast($items){returnend($items);}Exampleslast([1,2,3]);//3pluck检索给定键名的所有键值functionpluck($items,$key){returnarray_map(function($item)use($key){returnis_object($item)?$item->$key:$item[$key];},$items);}Examplespluck([['product_id'=>'prod-100','name'=>'Desk'],['product_id'=>'prod-200','name'=>'Chair'],],'name');//['Desk','Chair']pull修改原数组过滤掉指定值。函数pull(&$items,...$params){$items=array_values(array_diff($items,$params));return$items;}Examples$items=['a','b','c','a','b','c'];pull($items,'a','c');//$items将被['b','b']reject使用给定的回调过滤器数组。functionreject($items,$func){returnarray_values(array_diff($items,array_filter($items,$func)));}Examplesreject(['Apple','Pear','Kiwi','Banana'],函数($item){返回strlen($item)>4;});//['Pear','Kiwi']remove从给定函数返回false的数组中删除一个元素。函数删除($items,$func){$filtered=array_filter($items,$func);returnarray_diff_key($items,$filtered);}Examplesremove([1,2,3,4],function($n){return($n%2)===0;});//[0=>1,2=>3]tail返回数组中除第一个元素之外的所有元素。函数尾巴($items){返回计数($items)>1?array_slice($items,1):$items;}Exampletail([1,2,3]);//[2,3]take返回一个数组,从头开始移除n个元素。函数take($items,$n=1){returnarray_slice($items,0,$n);}Examplestake([1,2,3],5);//[1,2,3]取([1,2,3,4,5],2);//[1,2]不过滤除给定值以外的数组元素。没有函数($items,...$params){returnarray_values(array_diff($items,$params));}Exampleswithout([2,1,2,3,5,8],1,2,8);//[3,5]orderBy按键名对数组或对象集合进行排序。函数orderBy($items,$attr,$order){$sortedItems=[];foreach($itemsas$item){$key=is_object($item)?$item->{$attr}:$item[$attr];$sortedItems[$key]=$item;}if($order==='desc'){krsort($sortedItems);}else{ksort($sortedItems);}returnarray_values($sortedItems);}ExamplesorderBy([['id'=>2,'name'=>'Joy'],['id'=>3,'name'=>'Khaja'],['id'=>1,'name'=>'Raja']],'id','desc');//[['id'=>3,'name'=>'Khaja'],['id'=>2,'name'=>'Joy'],['id'=>1,'name'=>'Raja']]相关文章:30秒PHP代码片段(二)Math-Math30秒PHP代码片段(三)String-String&Function-函数
