Take the 2-minute tour ×

I have data from a form submission stored in a variable called $post_data. When I do print_r($post_data); I get the following array:

    [element_3] => John Doe
    [element_2] => john@example.com
    [element_14] => City
    [element_15] => Country
    [form_id] => 1
    [submit] => Submit

I want to store some of the fields in another array to pass to another script. Will my code below work? If not, how do I fix it?

$submitted_data = array(
    'Fields' => array(
            'Key' => 'Name',
            'Value' => $post_data['element_3']
            'Key' => 'Email',
            'Value' => $post_data['element_2']

Also, a PHP noob question, do I need another comma (,) in between the Name and Email array?


share|improve this question
do I need another comma (,) in between the Name and Email array? Yes, you do. Other than that, your solution looks fine. –  Decent Dabbler Mar 31 '11 at 8:18
will my code below work ? did you even try to test it before asking ?, and yes you need the comma as it it the separator between array elements. –  Serty Oan Mar 31 '11 at 8:19
I agree with Serty, indeed. Don't be scared to just try. And make sure you have enabled displaying of errors (only in your testing environment!) and set error_reporting to a decent level (E_ALL). –  Decent Dabbler Mar 31 '11 at 8:21
Why don’t you give your parameters meaningful names like “name” and “e-mail” rather than “element_3” and “element_2”? –  Gumbo Mar 31 '11 at 8:28
Yes it will work, provided you add a comma in between the Name and Email Array –  Sai Mar 31 '11 at 8:48

2 Answers 2

up vote 1 down vote accepted

I'm not exactly sure why you would want to do this, but depending on the field name you can consider using loops to help automate the entire process.

$field_map = array(
    'element_3'  => 'Name',
    'element_2'  => 'E-mail',
    'element_14' => 'City',
    'element_15' => 'Country'

$submitted_data = array('fields' => array());    
foreach ( $field_map as $key => $label) 
    $submitted_data['fields'][] = array(
        'key'   => $key,             // e.g. element_2
        'label' => $label,           // e.g. E-mail
        'value' => $post_data[$key]  // e.g. john@example.com

This separates the storage/mapping of key/label pairs from the part which processes it, making it easier to maintain and modify in the future.

share|improve this answer
Thanks, this helps make my code much more efficient –  blogjunkie Apr 2 '11 at 13:17

Another way might be (depending on how "fixed" the second script is, if you can alter it).


To get a result more like the one in your question:

share|improve this answer
Thanks, that explains exactly what I need! –  blogjunkie Mar 31 '11 at 9:50

Your Answer


