This time for everyone to bring the PHP Group Word algorithm implementation of the details of the PHP algorithm implementation of the note what, the following is the actual case, together to see.
<?php//Group Word algorithm function diywords ($arr, $m) { $result = array (); if ($m ==1) {//Only one word left, return $arr directly; } if ($m = = count ($arr)) { $result [] = Implode (', $arr); return $result; } $temp _firstelement = $arr [0]; unset ($arr [0]); $arr = Array_values ($arr); $temp _list1 = Diywords ($arr, ($m-1)); foreach ($temp _list1 as $s) { $s = $temp _firstelement. $s; $result [] = $s; } $temp _list2 = Diywords ($arr, $m); foreach ($temp _list2 as $s) { $result [] = $s; } return $result;} Group Word algorithm $arr=array (' pants ', ' denim ', ' low waist ', ' add fertilizer '), $count =count ($arr); for ($i =1; $i <= $count; $i + +) { $temp [$i]=diywords ($arr, $i);} Echo ' <pre/> ';p rint_r ($temp);
Operation Result:
Array
(
[1] = = Array
(
[0] = = Pants
[1] = = Cowboy
[2] + = low waist
[3] = + Fertilizer
)
[2] = = Array
(
[0] = = Pants Denim
[1] + = trousers low waist
[2] = + trousers Add Fertilizer
[3] = denim low waist
[4] = + Denim plus Fertilizer
[5] = low waist plus Fertilizer
)
[3] = = Array
(
[0] = pants Denim low waist
[1] = pants denim plus fat
[2] + pants low waist plus fat
[3] = denim low waist plus fertilizer
)
[4] = = Array
(
[0] = pants denim low waist add fertilizer
)
)
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP Implementation Huffman encoding/decoding steps
PHP+MONGODB database Operation steps