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

时间:2018-07-26 08:14:40

标签: php arrays cakephp multidimensional-array

在我的Cakephp表格中,我正在选择学生和多个文档。我的问题是我可以创建学生数组,但无法为上传的文件创建相同的数组。 我正在以这种格式获取值

[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
            )

    )

我想以以下格式创建数组。

[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
            )
    )

我正在使用以下命令获取阵列。我在学生数组上成功获取了结果,但是在生成文件名数组并将每个文件保存在服务器中时遇到了问题。 我的代码是

$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);
}

任何帮助将不胜感激,我真的对此深感困惑。 谢谢。

1 个答案:

答案 0 :(得分:0)

array_push($file,array_fill_keys($key, $files['name']));

如果您要填写数组的字段,它将起作用