lithium\util\Set::isNumeric PHP Method

isNumeric() public static method

Checks to see if all the values in the array are numeric.
public static isNumeric ( array $array = null ) : mixed
$array array The array to check. If null, the value of the current Set object.
return mixed `true` if values are numeric, `false` if not and `null` if the array to check is empty.
    public static function isNumeric($array = null)
    {
        if (empty($array)) {
            return null;
        }
        if ($array === range(0, count($array) - 1)) {
            return true;
        }
        $numeric = true;
        $keys = array_keys($array);
        $count = count($keys);
        for ($i = 0; $i < $count; $i++) {
            if (!is_numeric($array[$keys[$i]])) {
                $numeric = false;
                break;
            }
        }
        return $numeric;
    }

Usage Example

Beispiel #1
0
	public function testIsNumericArrayCheck() {
		$data = array('one');
		$this->assertTrue(Set::isNumeric(array_keys($data)));

		$data = array(1 => 'one');
		$this->assertFalse(Set::isNumeric($data));

		$data = array('one');
		$this->assertFalse(Set::isNumeric($data));

		$data = array('one' => 'two');
		$this->assertFalse(Set::isNumeric($data));

		$data = array('one' => 1);
		$this->assertTrue(Set::isNumeric($data));

		$data = array(0);
		$this->assertTrue(Set::isNumeric($data));

		$data = array('one', 'two', 'three', 'four', 'five');
		$this->assertTrue(Set::isNumeric(array_keys($data)));

		$data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
		$this->assertTrue(Set::isNumeric(array_keys($data)));

		$data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
		$this->assertTrue(Set::isNumeric(array_keys($data)));

		$data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
		$this->assertFalse(Set::isNumeric(array_keys($data)));

		$data = array();
		$this->assertNull(Set::isNumeric($data));
	}