dongli8466 2015-02-28 15:09
浏览 32

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

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.

  • 写回答

2条回答 默认 最新

  • doushenmao9036 2015-02-28 15:15
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图