Documentation

ArrayHelper
in package

FinalYes

Array utility.

Table of Contents

Methods

filteringElements()  : array<string|int, mixed>
Create an associative array, or an array of only the elements of any key from an associative array list.
getRandomValue()  : mixed
Randomly takes elements out of an array. The array passed as an argument is also modified.
grouping()  : array<string|int, mixed>
Grouping of associative arrays by specified key.
isVector()  : bool
Returns true if the subscript is an array starting from 0.
resetArrayKeys()  : array<string|int, mixed>
Reset array keys (0,1,2...).
searchArrayByKey()  : mixed|null
Searches for a value by key from an array.
toTable()  : string
Returns an array as a tabular string.

Methods

filteringElements()

Create an associative array, or an array of only the elements of any key from an associative array list.

public static filteringElements(array<string|int, mixed> $arr, mixed ...$includeKey) : array<string|int, mixed>
use \X\Util\ArrayHelper;

$staffs = [
  ['name' => 'Derek Emmanuel', 'regno' => 'FE/30304', 'email' => 'derekemmanuel@gmail.com'],
  ['name' => 'Rubecca Michealson', 'regno' => 'FE/20003', 'email' => 'rmichealsongmail.com'],
  ['name' => 'Frank Castle', 'regno' => 'FE/10002', 'email' => 'fcastle86@gmail.com'],
];
$staffs = ArrayHelper::filteringElements($staffs, 'name', 'email');
print_r($staffs);
// Array
// (
//     [0] => Array
//         (
//             [name] => Derek Emmanuel
//             [email] => derekemmanuel@gmail.com
//         )
//     [1] => Array
//         (
//             [name] => Rubecca Michealson
//             [email] => rmichealsongmail.com
//         )
//     [2] => Array
//         (
//             [name] => Frank Castle
//             [email] => fcastle86@gmail.com
//         )
// )

$staff = ['name' => 'Derek Emmanuel', 'regno' => 'FE/30304', 'email' => 'derekemmanuel@gmail.com'];
$staff = ArrayHelper::filteringElements($staff, 'name', 'email');
print_r($staff);
// Array
// (
//     [name] => Derek Emmanuel
//     [email] => derekemmanuel@gmail.com
// )
Parameters
$arr : array<string|int, mixed>

Array.

$includeKey : mixed

Key of the array element to be retrieved.

Return values
array<string|int, mixed>

Array.

getRandomValue()

Randomly takes elements out of an array. The array passed as an argument is also modified.

public static getRandomValue(array<string|int, mixed> &$arr) : mixed
Parameters
$arr : array<string|int, mixed>

Array.

Return values
mixed

Array elements.

grouping()

Grouping of associative arrays by specified key.

public static grouping(array<string|int, mixed> $arr, string $groupBy) : array<string|int, mixed>
use \X\Util\ArrayHelper;

$foods = [
  ['name' => 'Apple', 'category' => 'fruits'],
  ['name' => 'Strawberry', 'category' => 'fruits'],
  ['name' => 'Tomato', 'category' => 'vegetables'],
  ['name' => 'Carot', 'category' => 'vegetables'],
];
ArrayHelper::grouping($foods, 'category');
// [
//   'fruits' => [
//     ['name' => 'Apple', 'category' => 'fruits'],
//     ['name' => 'Strawberry', 'category' => 'fruits']
//   ],
//   'vegetables' => [
//     ['name' => 'Tomato', 'category' => 'vegetables'],
//     ['name' => 'Carot', 'category' => 'vegetables']
//   ],
// ]
Parameters
$arr : array<string|int, mixed>

Array.

$groupBy : string

Group key.

Return values
array<string|int, mixed>

Grouped arrays.

isVector()

Returns true if the subscript is an array starting from 0.

public static isVector(array<string|int, mixed> $arr) : bool
Parameters
$arr : array<string|int, mixed>

Array.

Return values
bool

Returns true for vector arrays.

resetArrayKeys()

Reset array keys (0,1,2...).

public static resetArrayKeys(array<string|int, mixed> $arr) : array<string|int, mixed>
Parameters
$arr : array<string|int, mixed>

Array.

Return values
array<string|int, mixed>

Array.

searchArrayByKey()

Searches for a value by key from an array.

public static searchArrayByKey(string $needle, array<string|int, mixed> $arr) : mixed|null
use \X\Util\ArrayHelper;

// Search from a simple array.
ArrayHelper::searchArrayByKey('France', [
  'France' => 'Paris',
  'UK' => 'London',
  'USA' => 'New York'
]);// => Paris

// Search from nested array.
ArrayHelper::searchArrayByKey('USA', [
  'cities' => [
    'France' => 'Paris',
    'UK' => 'London',
    'USA' => 'New York'
  ]
]);// => New York
Parameters
$needle : string

Key of the array to search.

$arr : array<string|int, mixed>

Array.

Return values
mixed|null

The value of the array found.

toTable()

Returns an array as a tabular string.

public static toTable(array<string|int, mixed> $arr) : string
use \X\Util\ArrayHelper;

$arr = [
  ['firstname' => 'John', 'lastname' => 'Mathew', 'email' => 'John.Mathew@xyz.com'],
  ['firstname' => 'Jim', 'lastname' => 'Parker', 'email' => 'Jim.Parker@xyz.com'],
];
echo ArrayHelper::toTable($arr);
┌───────────┬──────────┬─────────────────────┐
│ FIRSTNAME │ LASTNAME │        EMAIL        │
├───────────┼──────────┼─────────────────────┤
│ John      │ Mathew   │ John.Mathew@xyz.com │
│ Jim       │ Parker   │ Jim.Parker@xyz.com  │
└───────────┴──────────┴─────────────────────┘
Parameters
$arr : array<string|int, mixed>

Array.

Return values
string

Returns a tabular string.


        
On this page

Search results