duanlianyun0462 2016-09-22 21:35
浏览 43
已采纳

从php中的函数回显数组

I am trying to create an array from a function.

The function:

$results = [];
function getDirEnties($directory, &$results) {
    $entries = scandir($directory);
    foreach ($entries as $item) {
        if (!in_array($item, ['.', '..']) && substr($item, 0, 1) !== '.') {
            $path = $directory . '/' . $item;
            if (is_dir($path)) {
                getDirEnties($path, $results);
            } else {
                $pathInfo = pathinfo($path);
                $name = $pathInfo['filename'];
                $type = 'unknown';
                if (!empty($pathInfo['extension'])) {
                    $name .= "." . $pathInfo['extension'];
                    switch (strtolower($pathInfo['extension'])) {
                        case "gif":
                        case "jpg":
                        case "png":
                        case "jpeg":
                        case "bmp":
                        //etc..
                        $type = 'image';
                        break;
                        case "mp4":
                        $type = 'media';
                        break;
                    }
                }
            $data = [
                'name' => $name,
                'path' => $pathInfo['dirname'],
                'type' => $type,
                'time' => filemtime($path)
                ];
            $results[] = $data;
            }
        }
    }
    return $results;
} 

to populate the function I have the following:

$stmt=$db->prepare('SELECT friend2 as username FROM list_friends 
                    WHERE (friend1 = :username AND friend2 <> :username) 
                    UNION
                    SELECT friend1 as username FROM list_friends
                    WHERE (friend2 = :username AND friend1 <> :username)');
$stmt->bindParam(':username', $username);
$stmt->execute();
$friend_rows = $stmt->fetchAll();
foreach ($friend_rows as $rows) {
    $friend = strtolower($rows['username']);
    $directoryToScan = '/path/to/'.$friend;
    $tree = getDirEnties($directoryToScan, $results);
}

I am then trying to echo it out as follows:

function cmp($a, $b)  {
    $ad = new DateTime($a['time']);
    $bd = new DateTime($b['time']);   
    if ($ad == $bd) {
        return 0;
    }
return $ad < $bd ? -1 : 1;
}
if ($data !== NULL) {
    usort($data, "cmp");
    for($i=(count($data)-1)-($feed-1);$i>=(count($data)-10)-($feed-1);$i--){
        if($data[$i]['type']=='image'){ echo $data[$i]['name']; }
    }
}

I am JUST learning functions and have VERY limited experience with arrays, mostly with mysql arrays so I have no idea how to write this in such a way it returns the $data[] array properly as needed.

  • 写回答

3条回答 默认 最新

  • duanhao4156 2016-09-22 22:12
    关注

    You inverted the use of $results and $data

    Here you use your $results array as input/output :

    $tree = getDirEnties($directoryToScan, $results);
    

    Then later you try to print $data

    if ($data !== NULL) {
      usort($data, "cmp");
      for($i=(count($data)-1)-($feed-1);$i>=(count($data)-10)-($feed-1);$i--){
        if($data[$i]['type']=='image'){ echo $data[$i]['name']; }
      }
    }
    

    But the way you written the code, all your needs are in $results. Use $results !

    if ($results !== NULL) {
      usort($results, "cmp");
      for($i=(count($results)-1)-($feed-1);$i>=(count($results)-10)-($feed-1);$i--){
        if($results[$i]['type']=='image'){ echo $results[$i]['name']; }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料