Oihana PHP

toIntegers.php

Table of Contents

Functions

toIntegers()  : array<int, int>
Filters a value down to a list of integers, keeping every integer and every string that writes one, and dropping the rest.

Functions

toIntegers()

Filters a value down to a list of integers, keeping every integer and every string that writes one, and dropping the rest.

toIntegers(mixed $value) : array<int, int>

The value is first passed through toArray() (a bare scalar is wrapped as a single-element list), then walked : an int is kept as is ; a string is kept, cast to int, when it writes an integer and nothing else — an optional sign, digits, surrounding spaces allowed, leading zeros included ('007' gives 7). Anything else is dropped : a float — even 350.0 —, a string holding a decimal, an exponent or a hexadecimal number, a bool, null, an array, an object.

Unlike toInt(), which converts one value and falls back to 0, this filters a list : what is not an integer has no place in it, and a decimal is never truncated into one.

Parameters
$value : mixed

The value to filter — an array, or a bare scalar wrapped via toArray().

Tags
example
use function oihana\core\arrays\toIntegers;

toIntegers( [ 12 , '34' , ' 56 ' , '007' , 7.5 , '8.5' , true , null ] ) ; // [ 12 , 34 , 56 , 7 ]
toIntegers( 42 )                                                        ; // [ 42 ]
toIntegers( null )                                                      ; // []
author

Marc Alcaraz (ekameleon)

since
1.3.0
Return values
array<int, int> —

The kept items, as integers, in their order, duplicates included, re-indexed from 0.

On this page

Search results