2

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:

Array
(
    [element_3] => John Doe
    [element_2] => [email protected]
    [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(
        array(
            'Key' => 'Name',
            'Value' => $post_data['element_3']
        )
        array(
            'Key' => 'Email',
            'Value' => $post_data['element_2']
        )
    )
)

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

Thanks!

7
  • do I need another comma (,) in between the Name and Email array? Yes, you do. Other than that, your solution looks fine. Commented Mar 31, 2011 at 8:18
  • 2
    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. Commented Mar 31, 2011 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). Commented Mar 31, 2011 at 8:21
  • 1
    Why don’t you give your parameters meaningful names like “name” and “e-mail” rather than “element_3” and “element_2”? Commented Mar 31, 2011 at 8:28
  • Yes it will work, provided you add a comma in between the Name and Email Array Commented Mar 31, 2011 at 8:48

2 Answers 2

1

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. [email protected]
    );
}

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

$submitted_data['Name']=$post_data['element_3'];
$submitted_data['Email']=$post_data['element_2'];

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

$submitted_data['Fields']['0']['Key']='Name';
$submitted_data['Fields']['0']['Value']=$post_data['element_3'];
$submitted_data['Fields']['1']['Key']='Email';
$submitted_data['Fields']['1']['Value']=$post_data['element_2'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.