Jump to content

[SOLVED] Quick array question.......


lockdownd7

Recommended Posts

I've got an array of file names like:

 

$array[0] = blah.html

$array[1] = blah.jpg

$array[2] = blah.txt

$array[3] = blah2.txt

 

How would I count the number of times a particular file type appears; i.e. in the above example if I counted the number of .txt files, I'd get two.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/172768-solved-quick-array-question/
Share on other sites

<?php
$array = array("blah.html", "blah.jpg", "blah.txt", "blah2.txt");

$file_ext = array_count_file_types($array);

print(".txt appears ". $file_ext["txt"] ." times.");

function array_count_file_types($arr)
{
    $types = array();
    
    foreach($arr as $item)
    {
        $types[substr(strrchr(trim($item), '.'), 1)]++;
    }
    
    return $types;    
}
?>

 

Output

.txt appears 2 times.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.