Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP array help

  • 03-08-2012 9: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