get_array_types()

Since v1.3.0

Description

get_array_types() returns a list of all strict PHP types for a given PHP array. The list is ordered with the most specific match first.

public array get_array_types(mixed $item);

Parameters

The input parameters are:

Return Value

get_array_types() returns an array.

The resulting list is a complete list of strict types where it is safe to use $item.

Although PHP arrays and Traversable objects are safe to use in a foreach() loop, PHP 7.0 type declarations won't let you pass an array into a function or method that expects a Traversable.

That's why get_array_types() does not include Traversable in the returned list.

You can't pass an ArrayObject, ArrayAccess or a Traversable into any of the array_XXX() functions.

That's why get_array_types() returns an empty list for ArrayObject, ArrayAccess, and Traversable.

Examples

Here's a list of examples of accepted input values:

var_dump(get_array_types([1,2,3]));

// outputs
//
// array(1) {
//   ["array"]=>
//   string(5) "array"
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;

var_dump(get_array_types([GetStrictTypes::class, "from"]));

// outputs
//
// array(2) {
//   ["callable"]=>
//   string(8) "callable"
//   ["array"]=>
//   string(5) "array"
// }

Here's a list of examples of ingored input values:

var_dump(get_array_types(null));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(function(){}));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(true));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(false));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(0.0));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(3.1415927));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(0));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(100));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(-100));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(new ArrayObject));

// outputs
//
// array(0) {
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;

var_dump(get_array_types(new GetStrictTypes));

// outputs
//
// array(0) {
// }
var_dump(get_array_types((object)[]));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(STDIN));

// outputs
//
// array(0) {
// }
var_dump(get_array_types("true"));

// outputs
//
// array(0) {
// }
var_dump(get_array_types("false"));

// outputs
//
// array(0) {
// }
var_dump(get_array_types("0.0"));

// outputs
//
// array(0) {
// }
var_dump(get_array_types("3.1415927"));

// outputs
//
// array(0) {
// }
var_dump(get_array_types("0"));

// outputs
//
// array(0) {
// }
var_dump(get_array_types("100"));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(new Exception(__FILE__)));

// outputs
//
// array(0) {
// }
var_dump(get_array_types("hello, world!"));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(ArrayObject::class));

// outputs
//
// array(0) {
// }
var_dump(get_array_types(Traversable::class));

// outputs
//
// array(0) {
// }

Throws

GetArrayTypes does not throw any exceptions.

Works With

GetArrayTypes is supported on these versions of PHP:

PHP Version Works?
5.5 Yes
5.6 Yes
7.0 Yes
HHVM Yes