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

报告相同问题?

悬赏问题

  • ¥20 阿里云python代码求解
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路