dongshi4078 2018-07-26 08:14
浏览 52
已采纳

在cakephp中从多维数组创建数组

In my Cakephp Form I am Selecting Students And multiple documents. My Problem is that I can Create Student Array Fine But Can't able to create the same for uploaded file. i am getting the values in this format

[student_id] => Array
    (
        [0] => 1
        [1] => 2
    )
$student=['1','2'];


$filename= array(
  array(
    "tmp_name" => "C:\xampp\tmp\phpC542.tmp",
    "error" => "0",
    "name" => "file1.pdf",
    "type" => "application/pdf",
    "size" => "410922"
  ),
  array(
    "tmp_name" => "C:\xampp\tmp\phpC562.tmp",
    "error" => "0",
    "name" => "file2.pdf",
    "type" => "application/pdf",
    "size" => "410922"
  ),
  array(
    "tmp_name" => "C:\xampp\tmp\phpC545.tmp",
    "error" => "0",
    "name" => "file3.pdf",
    "type" => "application/pdf",
    "size" => "410922"
  )
);
[filename] => Array
    (
        [0] => Array
            (
                [tmp_name] => C:\xampp\tmp\php53EE.tmp
                [error] => 0
                [name] => file1.pdf
                [type] => application/pdf
                [size] => 403993
            )

        [1] => Array
            (
                [tmp_name] => C:\xampp\tmp\php53FE.tmp
                [error] => 0
                [name] => file2.pdf
                [type] => application/pdf
                [size] => 410922
            )

        [2] => Array
            (
                [tmp_name] => C:\xampp\tmp\php541F.tmp
                [error] => 0
                [name] => file3.pdf
                [type] => application/pdf
                [size] => 846448
            )

    )

I want to create the Array in the following format.

[students] => Array
    (
        [0] => Array
            (
                [student_id] => 1
            )

        [1] => Array
            (
                [student_id] => 2
            )

    )
[files] => Array
    (
        [0] => Array
            (
                [filename] => file1.pdf              
            )
        [1] => Array
            (
                [filename] => file2.pdf              
            )
        [2] => Array
            (
                [filename] => file3.pdf
            )
    )

I am Using the following to get my array. I am successfully getting the results on student array but having problem with generating filename array and saving each file in server. My code is

$data=$this->request->getData();
$studentids=$data['student_id'];
$students=array();
foreach($studentidsas $studentids){
    $key=array('student_id');
    array_push($students,array_fill_keys($key, $studentids));
}
$files=$data['filename'];
$file=array();
foreach($files as $files){
    $key=array('filename');
    array_push($file,array_fill_keys($key, $files));
    move_uploaded_file($data['filename']['tmp_name'],WWW_ROOT.'pdffiles//'.$filename);
}

Any Help will Really Appreciated i am really stuck in this. Thanks.

  • 写回答

1条回答 默认 最新

  • doulun1939 2018-07-26 08:39
    关注
    array_push($file,array_fill_keys($key, $files['name']));
    

    if you give the field you want to fill your array with, it will work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵