I'm extracting data and then creating an array like:
$foo = $_POST[ 'foo' ];
$bar = $_POST[ 'bar' ];
$val = array( 'foo' => $foo, 'bar' => $bar );
Is it possible to have a shortcut function?
Something like the following:
$val = array( someFunction( 'foo' ), someFunction( 'bar' ) );
Where:
function someFunction( $name ) {
return( "$name" => $_POST[ $name ] ); // Does not work!
}
You're thinking in the right direction, but not nearly big enough!
You want to define an input schema like:
$input = $_POST;
$input_schema = array(
"foo" => "string",
"bar" => "string"
);
validate_input($input, $schema);
From then on you can safely use $input
in accordance with your schema.
What you're trying to do with the Associative Array definition is impossible with the current PHP syntax:
array( someFunction( 'foo' ), someFunction( 'bar' ) );
However, this is the closest thing I can think of:
$_POST = array( 'foo' => 'FOO', 'bar' => 'BAR' );
$var = array();
someFunction( $var, 'foo' );
someFunction( $var, 'bar' );
function someFunction( &$output, $name ) {
$output[$name] = $_POST[$name];
}
Not very sexy and might be confusing also, but will do the job.
Update
Is it what you're looking for -- it is even closer:
$var = ( list($foo, $bar) = $_POST );
// OR
$var = (array) ( list($foo, $bar) = $_POST );
Obviously it's highly depended on the array structure, so it might not be a good idea to use it for $_POST
for example.