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

在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

    打赏 评论