【问题标题】:filter possibilities within a multidimensional array PHP/Codeigniter过滤多维数组 PHP/Codeigniter 中的可能性
【发布时间】:2013-01-16 22:03:06
【问题描述】:

在工作了将近一整天后,我的大脑陷入了如何解决这个问题的想法中。一个例子。

  1. 1,
  2. x,
  3. 2,
  4. 1x,
  5. 1,
  6. 2,
  7. x,
  8. 1,
  9. 2,
  10. 1,
  11. x,
  12. x,
  13. 1..

这是一张优惠券,我希望按照组合的可能性对其进行排序假设这张优惠券有两种可能性: 1x2112x121xx1, 1x2x12x121xx1..第四场比赛不一样..

所以...我的问题是如何以编程方式完成。

我从数据库中获取数据,它看起来像这样。

array(
    "gameNumber" => "1", 
    "bets" => array(
        [0] => "1"
    ), 
    "gameNumber" => "2", 
    "bets" =>  array(
        [0] = "x"
    ), 
    "gameNumber" => "3", 
    "bets" => array(
        [0] = "2"
    ), 
    "gameNumber" => "4", 
    "bets" => array(
        [0] = "1", 
        [1] = "x"
    )
);

我不确定这会对你有帮助,但看起来就是这样。

在最后一个答案的帮助下,我已将其全部放入一个递归数组中: How to build recursive function to list all combinations of a multi-level array?.

所以我的新递归函数如下所示:

public function _combine_bets($array)
{
    $cur = array_shift($array);
    $result = array();

    if(!count($array)) {
        foreach($cur['bets'] as $option) {
            $result[] = $cur['matchNumber']."-".$option;
        }
        return $result;
    }
    foreach($cur['bets'] as $bet) {
        $result[$cur['matchNumber'].'-'.$bet] = $this->_combine_bets($array);
    }
    return $result;
}

这样我得到了这个数组: http://pastie.org/5692408

Soo..... 回到我的问题,我想让这看起来只是赌注的价值.. 假设它应该看起来像这样:1x2112x121xx1、1x2x12x121xx1。而不是一个大数组。我需要为所有可能性做这个,这是一张只有两种不同可能性的优惠券,但是.. 它可以更多。

用户可以在比赛中选择 1,x,2,1x,12,x2,1x2 我的意思是用户在 13 种不同游戏中的所有选项,

如果您需要更多信息,请告诉我,我会更新此信息..

【问题讨论】:

    标签: php codeigniter recursion


    【解决方案1】:

    主要问题是为什么你想要一个递归函数? 这是一个没有递归的解决方案。

    public function _combine_bets($array) {
    
        $result = '';
        foreach($array['bets'] as $bet) {
            $result .= $bet;
        }
    // returns string
    return $result;
    }
    

    你想退回什么?为什么需要递归函数?

    我不明白这个问题。

    【讨论】:

    • 我认为你误解了我的问题。如果我使用您的功能,我将在第四场比赛中获得 1x。这就是它会给我的一切,它会给我每场比赛的赌注。我希望优惠券行像这样返回给我:array(0 => '1x2112x121xx1', 1 => '1x2x12x121xx1');
    • 而您将获得的优惠券是每个matchNumber的投注?如果数组中没有正确的游戏号码,则投注将被忽略?
    【解决方案2】:

    我把不可能的事情变成了可能,别问我是怎么做到的!

    function _combine_bets($array, $curBet = null, $currentRow = null)
    {
        $cur = array_shift($array);
        $result = array();
    
        if(!isset($currentRow))
            $currentRow = $curBet;
        else 
            $currentRow .= $curBet;
    
    
        if(!count($array)) {
            foreach($cur['bets'] as $bet) {
                $currentRow .= $bet;
                if(strlen($currentRow) == 13)
                    $this->m_couponRows[] = $currentRow;
                $currentRow = null;
            }
            return $this->m_couponRows;
        }
    
        foreach($cur['bets'] as $bet) {
            $result[$bet] = $this->_combine_bets($array, $bet, $currentRow);
        }
        return $this->m_couponRows;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2012-12-04
      • 2018-07-07
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多