Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP array help

  • 03-08-2012 10:29AM
    #1
    Registered Users, Registered Users 2 Posts: 872 ✭✭✭


    Hi,

    I have a number of radio buttons that post over values like option_41_200, where 41 is the account_id and 200 is the payment_id.

    Multiple payments for one account can be posted over at one time.

    I need to store the account_id and then a list of the payment_id's for processing later, so, something like
    $account_payments = array('41','201,202,203,204')
    

    I am having difficulty storing the items in the way i have outlined above. I just need to store one instance of the account_id and then concatenate all the payment_id's.
    
    $account_id = 0;
    $invoice_list = '';
    $collection = array();
    $i = 0;
    
    foreach($_POST as $key=>$value)
        	{
        		if(strpos($key, "option_")!==FALSE)
        		{
        			$value = explode('_',$key);
        			$account_id = $value[2];
        			
        			if($account_id == $value[2])
        			{
    	    			$account_id = $value[2];
    
    	    			$collection[$i][0] =  $value[2];
    	    			
    	    			$invoice_list .= $value[3] . ',';
    	    			$collection[$i][1] = $invoice_list;
        			}
        			
        			$i++;
        		}
        	}
    

    THis code is outputting the following which while quite close to what i want is incorrect
    Array
    (
        [0] => Array
            (
                [0] => 41
                [1] => 361,
            )
    
        [1] => Array
            (
                [0] => 42
                [1] => 361,21,
            )
    
        [2] => Array
            (
                [0] => 42
                [1] => 361,21,55,
            )
    
        [3] => Array
            (
                [0] => 42
                [1] => 361,21,55,89,
            )
    
    )
    

    Does anyone have any ideas on how i can get the desired output ?

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 342 ✭✭adm


    If you use the account id as a key in a new array you could
    then use array_key_exists() to append to that array item.

    Not sure how this works in your code:
    [PHP]
    $value = explode('_',$key);
    $account_id = $value[2];
    [/PHP]

    should it not be $account_id = $value[1]; ?


    anyway heres a possible solution

    [PHP]
    $account_id = 0;
    $invoice_list = '';
    $collection = array();

    //$_POST = array('option_41_100'=>'','option_40_200'=>'','option_41_250'=>'','option_43_300'=>'');


    foreach($_POST as $key=>$val)
    {
    if(strpos($key, "option_")!==FALSE)
    {
    $value = explode('_',$key);
    if (array_key_exists($value[1], $collection)) {
    $collection[$value[1]] .= $value[2] . ',';
    } else{
    $collection[$value[1]] = $value[2] . ',';
    }
    }
    }
    // To get $account_payments = array('41','201,202,203,204')
    $account_payments = array();
    foreach($collection as $key => $val){
    $account_payments[] = array($key , $val);
    }

    //print_r($account_payments);
    [/PHP]


  • Registered Users, Registered Users 2 Posts: 872 ✭✭✭grahamor


    Thanks alot adm, works perfect :)


Advertisement