配列内よりランダムに要素(例えば画像URL)を取得

配列

 閲覧数:573  投稿日:2023-11-22  更新日:2023-11-22  

配列内よりランダムに要素(例えば画像URL)を取得


コード

$imgAry='https://hoge1.jpg','https://hoge2.jpg','https://hoge3.jpg','https://hoge4.jpg'];
print_r(count($imgAry)); //4
$randomKey = random_int(0, count($imgAry)-1); 
print_r($imgAry[$randomKey]);



結果

'https://hoge2.jpg



タグ


random_int 


リンク


random_int



PHPで最後の「指定区切り文字」より後ろを取得

「Fatal error」を発生させないuser定義count関数