This time I will bring you a detailed explanation of the implementation of the PHP word grouping algorithm. What are the precautions for the implementation of the PHP word grouping algorithm. The following is a practical case, let's take a look.
<?php //组词算法 function diyWords($arr,$m){ $result = array(); if ($m ==1){//只剩一个词时直接返回 return $arr; } 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; } //组词算法 $arr=array('裤子','牛仔','低腰','加肥'); $count=count($arr); for($i=1;$i<=$count;$i++){ $temp[$i]=diyWords($arr,$i); } echo '<pre/>';print_r($temp);
Run result:
Array
(
[1] => Array
(
) [0] => Pants
# # # ] => Denim and fattening
. Waist
. 4] => Array
(
[0] => 裤子牛仔低腰加肥
)
)
相信看了本文案例你已经掌握For more exciting methods, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the steps to implement Huffman encoding/decoding in PHP
Detailed explanation of the steps for PHP mongoDB database operation
The above is the detailed content of Detailed explanation of PHP word grouping algorithm implementation. For more information, please follow other related articles on the PHP Chinese website!