/** * Should be able to tell if an array is purely indexed or associative */ public function testIsIndexed() { $test = explode(' ', 'Hello there Mister Donut!'); $this->assertTrue(Arrays::IsIndexed($test)); $test = [0 => 'Hello', 1 => 'There!', 2 => 'My Friend!']; unset($test[1]); $this->assertTrue(Arrays::IsIndexed($test)); $test = [0 => 'Hello', 2 => 'My Friend!', 1 => 'There!']; $this->assertFalse(Arrays::IsIndexed($test)); $test = [0 => 'Hello', true => 'There!']; $this->assertTrue(Arrays::IsIndexed($test)); // there's no remedy to determine if a key is int(1) or just boolean(1|true) $test = [0 => 'Hello', 'name' => 'Allen']; $this->assertFalse(Arrays::IsIndexed($test)); }
/** * Convert a runtime value into a virtual runtime value. Returns FALSE if value can't be determined. * * @param string $value Any scalar value, EXCEPT if it's an array. * @param boolean $throw [optional] If an exception will be thrown once a virtual runtime value can't be determined. This is useful when determining complex array values. * @return string|boolean */ public static function GetVirtual($value, $throw = true) { if (is_string($value)) { return String::Surround($value, "'"); } else { if (is_array($value)) { $closure_for_assoc = function ($current) { $newresult = []; foreach ($current as $key => $value) { array_push($newresult, sprintf("%s => %s", self::GetVirtual($key), $value)); // note that we didn't get Virtual value of $value since this has been previously virualized using array_map() } return $newresult; }; $result = array_map(function ($current) { return self::GetVirtual($current); }, $value); $result = str("[ {0} ]", implode(", ", Arrays::IsIndexed($result) ? $result : $closure_for_assoc($result))); return $result; } else { if (is_bool($value)) { return $value ? 'true' : 'false'; } else { if (is_null($value)) { return "null"; } else { if (is_scalar($value)) { return (string) $value; } } } } } if ($throw) { throw new ScalarValueException($value); } return false; }