Scandir senza punti in PHP - crea array con i file

Perfetto. Hai provato a fare scandir() su una directory e sono usciti i punti? Hai cercato all'infinito in rete i modi migliori per eludere questi noiosi segni di interpunzioni, ed il codice o non funziona, o è troppo lungo o ancora non sei capace di utilizzarlo...?!

Allora ecco il codice che fa a caso tuo:
$scandir=array_values(array_diff(scandir($dir),array('.','..'))); semplice, no?

array_diff(scandir($dir),array('.','..'))
Qui rimuove i punti e con array_values riassegna le chiavi dell'array, per ottenere da

array(22) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(26) "John Legend - Aim High.mp3"
[3]=>
string(27) "John Legend - All of Me.mp3"
[4]=>
string(52) "John Legend - Angel (Interlude) ft. Stacy Barthe.mp3"
[5]=>
string(24) "John Legend - Asylum.mp3"
[6]=>
string(27) "John Legend - Caught Up.mp3"
[7]=>
string(24) "John Legend - Dreams.mp3"
[8]=>
string(36) "John Legend - For The First Time.mp3"
[9]=>
string(32) "John Legend - Hold On Longer.mp3"
[10]=>
string(44) "John Legend - Love In The Future (Intro).mp3"
[11]=>
string(32) "John Legend - Made The Night.mp3"
[12]=>
string(30) "John Legend - Made To Love.mp3"
[13]=>
string(32) "John Legend - Open Your Eyes.mp3"
[14]=>
string(25) "John Legend - So Gone.mp3"
[15]=>
string(31) "John Legend - The Beginning.mp3"
[16]=>
string(26) "John Legend - Tomorrow.mp3"
[17]=>
string(32) "John Legend - Wanna Be Loved.mp3"
[18]=>
string(40) "John Legend - We Loved It feat. Seal.mp3"
[19]=>
string(48) "John Legend - What If I Told You (Interlude).mp3"
[20]=>
string(54) "John Legend - Who Do We Think We Are ft. Rick Ross.mp3"
[21]=>
string(47) "John Legend - You & I (Nobody In The World).mp3"
}

a ottenere

array(20) {
[0]=>
string(26) "John Legend - Aim High.mp3"
[1]=>
string(27) "John Legend - All of Me.mp3"
[2]=>
string(52) "John Legend - Angel (Interlude) ft. Stacy Barthe.mp3"
[3]=>
string(24) "John Legend - Asylum.mp3"
[4]=>
string(27) "John Legend - Caught Up.mp3"
[5]=>
string(24) "John Legend - Dreams.mp3"
[6]=>
string(36) "John Legend - For The First Time.mp3"
[7]=>
string(32) "John Legend - Hold On Longer.mp3"
[8]=>
string(44) "John Legend - Love In The Future (Intro).mp3"
[9]=>
string(32) "John Legend - Made The Night.mp3"
[10]=>
string(30) "John Legend - Made To Love.mp3"
[11]=>
string(32) "John Legend - Open Your Eyes.mp3"
[12]=>
string(25) "John Legend - So Gone.mp3"
[13]=>
string(31) "John Legend - The Beginning.mp3"
[14]=>
string(26) "John Legend - Tomorrow.mp3"
[15]=>
string(32) "John Legend - Wanna Be Loved.mp3"
[16]=>
string(40) "John Legend - We Loved It feat. Seal.mp3"
[17]=>
string(48) "John Legend - What If I Told You (Interlude).mp3"
[18]=>
string(54) "John Legend - Who Do We Think We Are ft. Rick Ross.mp3"
[19]=>
string(47) "John Legend - You & I (Nobody In The World).mp3"
}

ci sta in mezzo l'utilissimo codice che ho condiviso oggi con voi. C'è solo da definire in precedenza $dir con la stringa con il percorso da sondare. Ricordo che il percorsi delle cartelle finiscono sempre con /.

Alla prossima!

Totale articoli: 21. È lungo 466 parole, ed ha 4 commenti per adesso.