I've got the following $_POST
array after form submission and executing following line of code:
print_r($_POST); die;//Code to print the $_POST array
//Following is the output of above statement
Array
(
[fileName] => Array
(
[0] => 8.png
[1] => 2_OnClick_OK.jpg
)
[fileLink] => Array
(
[0] => https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd
[1] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ
)
[Submit] => Submit File
)
But actually I want a new array titled $request_arr
which after executing print_r($request_arr); die;
command should look like as follows:
Array
(
[8.png] => Array
(
[0] => https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd
)
[2_OnClick_OK.jpg]
(
[0] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ
)
)
N.B.:- For demonstration purpose only I've taken two elements. In actual scenario there could be hundreds of such elements. So please consider an optimum and efficient way to get this output array.
Thanks in advance.
If you have any query regarding the issue I'm facing please do let me know.
This should do what you want but I'm not sure why you would want that structure to your data unless you have something that is expecting that data format and you can't change it.
// Check that both array have same number of elements
if ( count($_POST[ 'fileName' ]) == count($_POST[ 'fileLink' ]) ) {
foreach ( $_POST[ 'fileName' ] as $key => $fn ) {
$request_arr[ $fn ][ 0 ] = $_POST[ 'fileLink' ][ $key ];
}
} else {
// You have bad data
}
The Solution to yours Problem would be the "array_combine" statement.
Useage/example:
$fname=array("a","b");
$age=array("35","37");
$c=array_combine($fname,$age);
print_r($c);
// Output would be " Array ( [a] => 35 [b] => 37 ) "
hope it helps