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 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题