如何以最佳有效的方式将$_POST数组转换为以下场景中所需的数组


How to convert the $_POST array into the desired array in following scenario in an optimum and efficient way?

在表单提交并执行以下代码行后,我得到了以下$_POST数组:

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
)

但实际上,我想要一个名为$request_arr的新数组,在执行print_r($request_arr); die;命令后,它应该如下所示:

Array
        (
            [8.png] => Array
                (
                    [0] => https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd
                )
            [2_OnClick_OK.jpg]
                (
                    [0] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ
                )   
        )

注意:-为了演示,我只选了两个元素。在实际场景中,可能有数百个这样的元素。因此,请考虑一种最佳且有效的方法来获得这个输出数组。

提前谢谢。

如果你对我面临的问题有任何疑问,请告诉我。

您的问题的解决方案是"array_component"语句。

用途/示例:

$fname=array("a","b");
$age=array("35","37");
$c=array_combine($fname,$age);
print_r($c);
// Output would be " Array ( [a] => 35 [b] => 37 ) "

希望它能帮助

这应该是你想要的,但我不确定你为什么要在数据中使用这种结构,除非你有期望的数据格式,并且你无法更改它。

// 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
}