Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

php - how do i prepend to an array

I would like to add /images/ to my array output and remove . and .. from the start of the array as well eg.

from

Array ( [0] => . [1] => .. [2] => 15216151220552.jpg [3] => 15542647385762.png [4] => 15544894045853.jpg [5] => 15704627111960.jpg [6] => 15738494275643.jpg [7] => 15908484237853.jpg [8] => 15908493560753.jpg [9] => 163d7e3.jpg [10] => 1_44_c.png [11] => 1a152e0.jpg [12] => 20200909_110828.jpg [13] => 20be31a.jpg [14] => 28639f0.jpg [15] => 468db69.jpg [16] => 51d4aba.jpg [17] => 5a73df5.jpg [18] => 5cb3a91.jpg [19] => 666a4b9.jpg [20] => 7120d2a.jpg )

to

Array ( [0] => /images/15216151220552.jpg [1] => /images/15542647385762.png [2] => /images/15544894045853.jpg [3] => /images/15704627111960.jpg [4] => /images/15738494275643.jpg [5] => /images/15908484237853.jpg [6] => /images/15908493560753.jpg [7] => /images/163d7e3.jpg [8] => /images/1_44_c.png [9] => /images/1a152e0.jpg [10] => /images/20200909_110828.jpg [11] => /images/20be31a.jpg [12] => /images/28639f0.jpg [13] => /images/468db69.jpg [14] => /images/51d4aba.jpg [15] => /images/5a73df5.jpg [17] => /images/5cb3a91.jpg [17] => /images/666a4b9.jpg [18] => /images/7120d2a.jpg )

the array is an output from

<?php
$dir0 = 'C:UsersexamplePicturesdesktopackgrounds';
$scan0 = scandir($dir0);
?>
question from:https://stackoverflow.com/questions/65651087/how-do-i-prepend-to-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Those are the current (.) and parent (..) directories. They are present in all directories, and are used to refer to the directory itself and its direct parent.

$array = array_diff(scandir($dir), ['..', '.']);

to add 'images':

$arrayWithPrefix = substr_replace($array, 'images/', 0, 0);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...