Created
November 21, 2016 20:08
-
-
Save afaur/446cd4bebce7813c1633c5b5dba3b221 to your computer and use it in GitHub Desktop.
PHP Form Field Mapping Replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$search_by_keys = array( | |
'foo', | |
'bar', | |
'baz' | |
); | |
$replace_with_keys = array( | |
'meow', | |
'rawr', | |
'ruff' | |
); | |
$original_form_fields_with_values = array( | |
'foo'=>'value', | |
'bar'=>'value', | |
'baz'=>'value' | |
); | |
$new_form_fields_with_values = array(); | |
// Iterate over all form fields to build a new form key=>value object | |
foreach( $original_form_fields_with_values as $original_key => $value ) { | |
// See if one of our field mappings needs to be applied | |
if ( in_array( $original_key, $search_by_keys ) ) { | |
// Find out which replacement we need to use by finding the array position | |
$replacement_index = array_search( $original_key, $search_by_keys ); | |
// Get the key with the replacement field name | |
$replacement_fieldname = $replace_with_keys[ $replacement_index ]; | |
// Put the corrected fieldname in the new form key=>value array | |
$new_form_fields_with_values[ $replacement_fieldname ] = $value; | |
} else { | |
// No Op copy field=>value form data | |
$new_form_fields_with_values[ $original_key ] = $value; | |
} | |
} | |
var_dump( $new_form_fields_with_values ); |
<?php
$replace = array(
'foo' => 'meow',
'bar' => 'rawr',
'baz' => 'ruff',
'wat' => 'nunu',
);
$original_form_fields_with_values = array(
'foo'=>'value',
'bar'=>'value',
'baz'=>'value',
'hehe' => 'blaine',
);
$new_form_fields_with_values = array();
foreach( $original_form_fields_with_values as $key => $value ) {
if ($replace[$key]) {
$new_form_fields_with_values[$replace[$key]] = $value;
} else {
$new_form_fields_with_values[$key] = $value;
}
}
var_dump( $new_form_fields_with_values );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about this: