LoginSignup
3

More than 3 years have passed since last update.

【PHP】配列内の要素が全て数値か調べる

Last updated at Posted at 2015-07-06

概要

配列内の要素が全て数値か調べる。
is_numeric は数値形式の文字列も true とみなすので注意。

code

is_numeric_array.php
function is_numeric_array($array)
{
    if (in_array(false, array_map('is_numeric', $array))) {
        return false;
    } else {
        return true;
    }
}
/* 実行結果 */
$array = array(
    '1',
    1,
    (object)'qiita',
    array(),
);
var_dump(is_numeric_array($array));
// boolean false

$array2 = array(
    '1',
    1,
);
var_dump(is_numeric_array($array2));
// boolean true

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3