3

after working almost an entire day my brain got stuck in my own thought about how to fix this problem. One example.

  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..

That's one coupon, I want it to be sorted after the possibilities of the combinations let's say this coupon has 2 possibilities those are: 1x2112x121xx1, 1x2x12x121xx1.. the fourth match is the one that's different..

So... my question is how should this be done programmatly.

I get the data from a database and it looks like this.

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"
    )
);

I'm not sure that will help you but that's how it looks.

I've got it all into a recursed array with help of the last answer of this: How to build recursive function to list all combinations of a multi-level array?.

So my new recursion function looks like this:

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;
}

This way I got this array as a result: http://pastie.org/5692408

Soo..... back to my question I want to make this look like only the value of the bet.. let's say it should look like this: 1x2112x121xx1, 1x2x12x121xx1. instead of a big array. I need to make this for all possibilites this is a coupon on just 2 different possibilities, but.. it can be alot more.

a user can choice 1,x,2,1x,12,x2,1x2 on a match I meant that's all options a user has on 13 different games,

if you need more information just tell me and I will update this..

Community
  • 1
  • 1
murum
  • 575
  • 4
  • 19

2 Answers2

1

The main question is why you want a recursive function? This is a solution without recursive-ness.

public function _combine_bets($array) {

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

What do you want returned? Why do you need a recursive function?

I don't understand the question.

Ms01
  • 4,420
  • 9
  • 48
  • 80
  • I think you are missunderstanding my question. If I use your function I will get 1x on the forth match. that's all it will give me back, it will give me the bets for each match. I want the coupon rows back to me like this: array( 0 => '1x2112x121xx1', 1 => '1x2x12x121xx1' ); – murum Jan 16 '13 at 11:59
  • and the coupon that you will get back is the bets for each matchNumber? If the array doesn't have the right game number, the bets are to be ignored? – Ms01 Jan 16 '13 at 13:33
1

I made the impossible thing possible, don't ask me how!

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;
}
murum
  • 575
  • 4
  • 19