Home > Backend Development > PHP Tutorial > php 递归函数 求解,该如何处理

php 递归函数 求解,该如何处理

WBOY
Release: 2016-06-13 13:11:10
Original
833 people have browsed it

php 递归函数 求解
本来按照想法,递归函数用测试数组返回来的应该是一个有2个值的二维数组。但值得到了一个数组,加红部分是不是不能这么写,那要怎么写呢?求解
================================代码如下所示=============================================================
$strr =array("周四301_菲尼克斯水星_芝加哥天空_主胜_2.10","周二302_明尼苏达山猫_印第安纳狂热_主胜_1.20","周二302_明尼苏达山猫_印第安纳狂热_客胜_2.92");

//这是分割数组的方法-----
function A($str){
//定义一个二维数组来存放根据逻辑分割出来的array
$newstr=array();
 for($i=0;$i   $e1=explode("_",$str[$i]);
  $e2=explode("_",$str[$i+1]);
  if($e1[0].$e1[1].$e1[2]==$e2[0].$e2[1].$e2[2]){
  //先分割成(两个)数组再在递归调用A($str)
  A(array_splice($str,$i,$i));
A(array_splice($str,$i+1,$i+1));
}
  }
  array_push($newstr,$str);
 return $newstr;
 }
//------------------------
$back=A($strr);
print_r($back);
?>
==========================================================================================================

------解决方案--------------------
发现还是不需要递归

PHP code

<?php $strr =array("第一场301_菲尼克斯水星_芝加哥天空_主胜_2.10","第一场301_菲尼克斯水星_芝加哥天空_客胜_12.10","第二场301_菲尼克斯水星_芝加哥天空_主胜_2.10","第二场301_菲尼克斯水星_芝加哥天空_客胜_12.10","第三场302_明尼苏达山猫_印第安纳狂热_主胜_1.20","第三场302_明尼苏达山猫_印第安纳狂热_客胜_2.92");

function B($list) {
    $midList = array();
    foreach($list as $line) {
        preg_match_all('/((.*?_.*?_.*?)_(.*))/', $line, $match, PREG_SET_ORDER);
        $match = $match[0];

        if(!isset($midList[$match[2]])) {
            $midList[$match[2]] = array();
        }
        $midList[$match[2]][] = $match[1];

    }
    $newList = array();
    // var_dump($midList);exit;
    $i = 0;
    foreach($midList as $match => $oddses) {
        // $newList[] = array();
        $newList = _combinate($newList, $oddses);

    }
    
    return $newList;
    
    
    
}
function _combinate($arr, $newItems) {
    $newArr = array();
    if(empty($arr)) {
        foreach($newItems as $item) {
            $newArr[] = array($item);
        }
        return $newArr;
    }
    
    foreach($newItems  as $newItem) {
        foreach($arr as $oldItems) {
            $oldItems[] = $newItem;
            $newArr[] = $oldItems;
        }
    }
    return $newArr;
}




echo '<pre class="brush:php;toolbar:false">';
print_r(B($strr));
exit;
<br><font color="#e78608">------解决方案--------------------</font><br>那只是你的数据给的不太适合,排一下序就可以了<br>我并不知道你想干什么!只是根据你的代码来猜测你的思路。要是离开你的整体构思,就没有意义了<br>
Copy after login
PHP code
$str_2=array("a_a_a_1","a_a_a_2","b_b_b_1","b_b_b_2","c_c_c_1","a_a_a_3");
//------------------------
$back = A($str_2);
print_r($back);

//这是分割数组的方法-----
function A($str){
  sort($str); //排序,把相似的摆在一起
  //定义一个二维数组来存放根据逻辑分割出来的array
  $newstr = array();
  for($i=0;$i<count explode if array_splice array_merge a array_push return class="clear">
                 
              
              
        
            </count>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template