如何将Phalcon文件对象的数组转换为简单的数组数组?

时间:2019-06-20 08:41:52

标签: php arrays json phalcon

我试图将文件对象数组转换为数组数组,但是我总是得到空结果。我尝试了下面的代码,但它给了我一个空数组

$json  = json_encode($this->request->getUploadedFiles());
$array = json_decode($json, true);
var_dump($array);exit;

以上var_dump的结果

array(2) { [0]=> array(0) { } [1]=> array(0) { } }

POST请求中的文件对象数组

array(2) { 
    [0]=> object(Phalcon\Http\Request\File)#695 (8) { 
        ["_name":protected]=> string(52) "27657439_10157161194752222_6818734335050869731_n.jpg" 
        ["_tmp":protected]=> string(24) "D:\xampp\tmp\php145A.tmp" 
        ["_size":protected]=> int(31591) 
        ["_type":protected]=> string(10) "image/jpeg" 
        ["_realType":protected]=> NULL 
        ["_error":protected]=> int(0) 
        ["_key":protected]=> string(9) "path_8" 
        ["_extension":protected]=> string(3) "jpg" 
    } 
    [1]=> object(Phalcon\Http\Request\File)#700 (8) { 
        ["_name":protected]=> string(8) "asif.PNG" 
        ["_tmp":protected]=> string(24) "D:\xampp\tmp\php145B.tmp" 
        ["_size":protected]=> int(425449) 
        ["_type":protected]=> string(9) "image/png" 
        ["_realType":protected]=> NULL 
        ["_error":protected]=> int(0) 
        ["_key":protected]=> string(9) "path_14" 
        ["_extension":protected]=> string(3) "PNG" 
    } 
}

我需要如下所示的结果数组:

array(2) { 
    ["path_8"]=> array(2) { 
        [0]=> array(5) { 
            ["error"]=> int(0) 
            ["name"]=> string(14) "image-name.jpg" 
            ["type"]=> string(10) "image/jpeg" 
            ["tmp_name"]=> string(14) "/tmp/phpqImhgm" 
            ["size"]=> int(222301) 
        } 
        [1]=> array(5) { 
            ["error"]=> int(0) 
            ["name"]=> string(42) "WhatsApp Image 2018-02-20 at 18.48.13.jpeg" 
            ["type"]=> string(10) "image/jpeg" 
            ["tmp_name"]=> string(14) "/tmp/php6HAWpJ" 
            ["size"]=> int(84153) 
        } 
    } 
    ["path_14"]=> array(1) { 
        [0]=> array(5) { 
            ["error"]=> int(4) 
            ["name"]=> string(0) "" 
            ["type"]=> string(0) "" 
            ["tmp_name"]=> string(0) "" 
            ["size"]=> int(0)
         } 
    }
}

更新:var_dump($ json)结果

string(7) "[{},{}]"

和var_dump($ this-> request-> getUploadedFiles());退出;结果是

array(2) { 
    [0]=> object(Phalcon\Http\Request\File)#673 (8) { 
        ["_name":protected]=> string(52) "27657439_10157161194752222_6818734335050869731_n.jpg" 
        ["_tmp":protected]=> string(24) "D:\xampp\tmp\php33AF.tmp" 
        ["_size":protected]=> int(31591) 
        ["_type":protected]=> string(10) "image/jpeg" 
        ["_realType":protected]=> NULL 
        ["_error":protected]=> int(0) 
        ["_key":protected]=> string(9) "path_8" 
        ["_extension":protected]=> string(3) "jpg" 
    } 
    [1]=> object(Phalcon\Http\Request\File)#695 (8) { 
        ["_name":protected]=> string(8) "asif.PNG" 
        ["_tmp":protected]=> string(24) "D:\xampp\tmp\php33BF.tmp" 
        ["_size":protected]=> int(425449) 
        ["_type":protected]=> string(9) "image/png" 
        ["_realType":protected]=> NULL 
        ["_error":protected]=> int(0) 
        ["_key":protected]=> string(9) "path_14" 
        ["_extension":protected]=> string(3) "PNG" 
    } 
}

1 个答案:

答案 0 :(得分:1)

最简单的方法是像这样解析:

$files = [];
foreach($this->request->getUploadedFiles() as $file){
   $item['name']     = $file->getName();
   $item['realType'] = $file->getRealType();
   $item['size']     = $file->getSize();
   ......................................
   $files[] = $item;
}

或者您可以重载File类: https://github.com/phalcon/cphalcon/blob/master/phalcon/Http/Request/File.zep 要实现ArrayAccess,还需要重载Request类。