1

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.

2
  • Where is $data originally defined? The first time I see it (outside the function at the top) is in the last code block. Based on what you have written, $data IS null in the last if statement Commented Sep 22, 2016 at 21:44
  • As I said I am still very new to functions and arrays. I thought I could output $results[] as an array to create the $data[] array. Perhaps I am doing the entire function wrong and that is why I can't echo results properly. Commented Sep 22, 2016 at 22:01

3 Answers 3

1

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']; }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

considering I using a sort function to sort out dozens of data[] arrays by time, doing this using a data[] array is somewhat important. Is it really as easy as just switching out $data and $results? going to try now.
0

If you want to print the entire array, use this. print_r(array_values($ARR));

6 Comments

want to try that again?
test it, then tell me that again
printr is not a php function
Sorry, its print_r
Why would you exclude the key? That is like 'the address' of the value...
|
0

The answer I marked correct, basically laid out my answer for me although it wasn't "exactly" what I wanted in terms of code. Ultimately all I had to do was change my variables and output a touch.

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

to

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

then

echo $tree; // I can't believe I missed this

then

return $results;

to

return $data;

Finally because I am sorting times with usort I had to change

'time' => filemtime($path)

to

'time' => date('F d Y h:i A', (filemtime($path)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.