dongtaotao19830418 2013-07-26 11:43
浏览 13
已采纳

用foreach构建的php数组

I'm making a class that lists files of a given extension inside a folder.

the trouble I'm having is that some files are images and I want to extract more information from those files and not for files that are not images.

I created this foreach that adds an entry to an array. later that array is returnd and contains all the files:

$file_array[] = array(
   "basename"=>$file->getBasename(),
   "size"=>$file->getSize(), //in bytes
   "extension"=>$extension,
   "file"=>$filename,
   "path"=>$file->getPath(),
   in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? array("info"=>getimagesize($filename)) : null
   );

As i said earlier, this works. the problem that I want to correct is the output:

Array
(
    [0] => Array
        (
            [basename] => 1.JPG
            [size] => 56533
            [extension] => jpg
            [file] => folder\1.JPG
            [path] => folder
            [0] => Array
                (
                    [info] => Array
                        (
                            [0] => 897
                            [1] => 616
                            [2] => 2
                            [3] => width="897" height="616"
                            [bits] => 8
                            [channels] => 3
                            [mime] => image/jpeg
                        )

                )

        )

    [1] => Array
        (
            [basename] => 2.JPG
            [size] => 56533
            [extension] => jpg
            [file] => folder\2.JPG
            [path] => folder
            [0] => Array
                (
                    [info] => Array
                        (
                            [0] => 897
                            [1] => 616
                            [2] => 2
                            [3] => width="897" height="616"
                            [bits] => 8
                            [channels] => 3
                            [mime] => image/jpeg
                        )

                )

        )
)

the problem I want to correct is that I want to access the file info like $file_array[0]['info'] and not $file_array[0][0]['info']. That second index is useless since there can be only one info for a file.

Any way to correct this?

  • 写回答

3条回答 默认 最新

  • dongtuo5611 2013-07-26 11:46
    关注

    Then don't add an array around your info key:

    'info' => in_array(...) ? getimagesize($filename) : null
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么