Warning: array_rand() [function.array-rand]: Second argument has to be between 1

エラーメッセージWarning

 状態:-  閲覧数:3,724  投稿日:2012-02-22  更新日:2013-03-07  
Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array

■想定原因1
array_rand関数第2引数で、指定した「取得エントリ数」が、配列の要素数より多かったため

■例1
$target = array("a","b","c","d");
(array_rand($target,5));

■修正例1
$target = array("a","b","c","d");
(array_rand($target,4));
※配列要素数1~4範囲内であれば、どれでもO.K.



■想定原因2
array_rand関数第2引数で、「取得エントリ数」に0を指定

■例1
$target = array("a","b","c","d");
(array_rand($target,0));

■修正例1
$target = array("a","b","c","d");
(array_rand($target,4));
※配列要素数1~4範囲内であれば、どれでもO.K.


Warning: Division by zero

Warning: strlen() expects parameter 1 to be string, array given in ○○.php on line △△

コメント投稿(ログインが必要)



類似度ページランキング
順位 ページタイトル抜粋
1 Warning: array_rand() [function.array-rand]: Second argument has to be between 1 38
2 SHA-1(Secure Hash Algorithm 1) 33
3 ( ! ) Warning: Declaration of xxxx should be compatible with 31
4 Warning: include(**.php) [function.include]: failed to open stream: No such file 29
5 Strict Standards: DateTime::__construct() [function.DateTime---construct]: It is 29
6 register_shutdown_function 28
7 Warning: include() [function.include]: Failed opening '**.php' for inclusion (in 28
8 Fatal error: Uncaught Error: Call to a member function modify() on string 27
9 ( ! ) Warning: file_put_contents(/a/b.jpg): failed to open stream: No such file or directory in 27
10 Parse error: syntax error, unexpected 'new' (T_NEW) 27
11 #1548 - Cannot load from mysql.proc. The table is probably corrupted 27
12 Sorry, that page does not exist. 27
13 Fatal error: Uncaught Error: Call to undefined function bcsub() 27
14 #1016 - Can't open file: './database_name/#sql-38f_36aa.frm' (errno: 24) 26
15 Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 26
16 Parse error: syntax error, unexpected 'public' (T_PUBLIC) 26
17 Fatal error: require_once(): Failed opening required 'XML/RSS.php' 26
18 Warning: session_write_close(): Failed to write session data (user) 26
19 Warning: mb_convert_encoding(): Unable to detect character encoding 26
20 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ○○.php on line △△ 26
2025/5/31 13:43 更新
週間人気ページランキング / 5-24 → 5-30
順位 ページタイトル抜粋 アクセス数
1 PHPにおけるメソッドのオーバーライドについて /「引数の数や型は、親クラスのメソッドと完全に一致していなければなりません。」とは具体的にどういう意味ですか? 5
2 http_build_queryとは? / 構文 /パラメータ 4
2 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 4
3 セッション管理が必要な理由は、HTTPプロトコルには状態を保持する機能がないため | セッション 3
3 htmlspecialchars / htmlentities / addslashes / mysql_real_escape_string / mysqli_real_escape_string | セキュリティ 3
3 ブラウザを閉じたらセッションデータはどうなるの? | セッション 3
4 ( ! ) Fatal error: Uncaught PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column | Fatal error(エラーメッセージ) 2
4 set_error_handler | 例外処理(制御構造) 2
4 $this | クラスとオブジェクト 2
4 Fatal error(エラーメッセージ) カテゴリー 2
4 PHPで定数を定義する方法は2種類ある / 配列定数の定義 2
4 Warning: session_write_close(): Failed to write session data (user) | Warning(エラーメッセージ) 2
4 ( ! ) Warning: Declaration of xxxx should be compatible with | エラーメッセージ 2
4 ( ! ) Fatal error: Uncaught Abraham\TwitterOAuth\TwitterOAuthException: {"errors":[{"code":32,"message":"Could not authenticate you."}]} | Fatal error(エラーメッセージ) 2
4 インポートするデータを受信できませんでした。ファイル名が送信されていないか、ファイルサイズが PHP の設定で許可された最大値を超えています。FAQ 1.16 をご覧ください | エラーメッセージ 2
4 Fatal error: require_once(): Failed opening required 'PEAR.php' | Fatal error(エラーメッセージ) 2
4 Fatal error: Uncaught Wrench\Exception\HandshakeException: No accept header receieved on handshake response in ★★★ headless-chromium-php/vendor/wrench/wrench/lib/Wrench/Protocol/Protocol.php | エラーメッセージ 2
4 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 2
4 可変変数 | 変数 2
4 クロージャ | 関数 2
2025/5/31 1:01 更新