PLZ: A handy PHP library for lazy programmers
  • Namespace
  • Class
  • Tree
  • Todo

Namespaces

  • amekusa
    • plz
  • PHP

Classes

  • alt
  • arr
  • constant
  • dom
  • fn
  • num
  • obj
  • op
  • path
  • str
  • sys
  • T
  • type
  • xml

Exceptions

  • ErrorException

Class arr

Array utilities

To get started, place the following line around top of your code.

use amekusa\plz\arr;
Abstract
Namespace: amekusa\plz
Located at arr.php

Methods summary

public static boolean
# eq( array|object $X, array|object $Y )

Returns whether or not $X equals $Y

Returns whether or not $X equals $Y

Specifically:

  • Tests each elements with amekusa\plz\op::eq()
  • Recurses a multidimensional array
  • Treats an object as an array with amekusa\plz\type::arr()

Parameters

$X
A variable to compare with $Y
$Y
A variable to compare with $X

Returns

boolean

Example

Comparing arrays

$X = array ('A', 'B', 'C');
$Y = array ('a', 'b', 'c');
$Z = array ('A', 'B', 'C');

echo 'Does $X equal to $Y? - ';
echo arr::eq($X, $Y) ? 'Yes.' : 'No.';
echo "\n";
echo 'Does $X equal to $Z? - ';
echo arr::eq($X, $Z) ? 'Yes.' : 'No.';
Does $X equal to $Y? - No.
Does $X equal to $Z? - Yes.

Comparing array-like objects

$X = new ArrayObject(array ('A', 'B', 'C'));
$Y = new ArrayObject(array ('a', 'b', 'c'));
$Z = new ArrayObject(array ('A', 'B', 'C'));

echo 'Does $X equal to $Y? - ';
echo arr::eq($X, $Y) ? 'Yes.' : 'No.';
echo "\n";
echo 'Does $X equal to $Z? - ';
echo arr::eq($X, $Z) ? 'Yes.' : 'No.';
Does $X equal to $Y? - No.
Does $X equal to $Z? - Yes.

public static integer
# count( array|object $X, boolean $Recursive = false )

Returns the number of elements in $X

Returns the number of elements in $X

Additionally:

  • If $X is uncountable, 1 is returned.
  • If $X is null, 0 is returned.

Parameters

$X
An array, countable object, or iterable object
$Recursive
(optional) Whether or not to count recursively

Returns

integer

Example

Demonstration

$var1 = array ('A', 'B');                       // Array
$var2 = new ArrayObject(array ('A', 'B', 'C')); // Countable object
$var3 = 'ABCD';                                 // String
$var4 = null;                                   // Null
var_dump( arr::count($var1) );
var_dump( arr::count($var2) );
var_dump( arr::count($var3) );
var_dump( arr::count($var4) );
int(2)
int(3)
int(1)
int(0)

Recursively counting

$var = array (
  'A', 'B',
  array ('C', 'D'),
  'E', 'F'
);
var_dump( arr::count($var)       ); // Normal
var_dump( arr::count($var, true) ); // Recursive
int(5)
int(7)

public static mixed
# first( array|object $X )

Returns the first element of $X

Returns the first element of $X

CAUTION: Calling in a foreach loop over $X can cause unpredictable results.

Parameters

$X
An array or an iterable object

Returns

mixed

Example

Demonstration

$var1 = array ('A', 'B', 'C');                  // Array
$var2 = new ArrayObject(array ('A', 'B', 'C')); // Iterable object
var_dump( arr::first($var1) );
var_dump( arr::first($var2) );
string(1) "A"
string(1) "A"

public static mixed
# last( array|object $X )

Returns the last element of $X

Returns the last element of $X

CAUTION: Calling this in a foreach loop over $X can cause unpredictable results.

Parameters

$X
An array or an iterable object

Returns

mixed

Example

Demonstration

$var1 = array ('A', 'B', 'C');                  // Array
$var2 = new ArrayObject(array ('A', 'B', 'C')); // Iterable object
var_dump( arr::last($var1) );
var_dump( arr::last($var2) );
string(1) "C"
string(1) "C"

public static boolean
# has_key( array|object $X, mixed $Key )

Returns whether $X has the supplied key

Returns whether $X has the supplied key

Parameters

$X
An array, array-like object, or traversable object
$Key
A key to find

Returns

boolean

Example

Demonstration

$var = array (
  'X' => 'A',
  'Y' => 'B',
  'Z' => 'C'
);
var_dump( arr::has_key($var, 'X') );
var_dump( arr::has_key($var, 'W') );
bool(true)
bool(false)

public static mixed
# get( array|object $X, mixed $Key, mixed $Alt = null )

Returns $X’s element indexed by $Key

Returns $X’s element indexed by $Key

If the element doesn’t exist, returns $Alt.

Parameters

$X
An array, array-like object, or traversable object
$Key
The key of an element to be returned
$Alt
(optional) An alternative value to return if $X doesn’t have the key

Returns

mixed

Example

Demonstration

$var = array (
  'X' => 'A',
  'Y' => 'B',
  'Z' => 'C'
);
var_dump( arr::get($var, 'X')                 ); // Same as $var['X']
var_dump( arr::get($var, 'W')                 ); // Alternates with NULL
var_dump( arr::get($var, 'W', 'No such key!') ); // Alternates with a string
string(1) "A"
NULL
string(12) "No such key!"

public static array
# flat( mixed $X )

Treats arguments as an one-dimensional array

Treats arguments as an one-dimensional array

Parameters

$X
Any number of parameters are accepted

Returns

array

Example

Converting a multi-dimensional array into one-dimensional

$var = array (
  'A',
  array (
    'B',
    array (
      'C'
    ),
    'D'
  ),
  'E'
);
var_export( arr::flat($var) );
array (
  0 => 'A',
  1 => 'B',
  2 => 'C',
  3 => 'D',
  4 => 'E',
)

Converting multiple arguments into an one-dimentional array

$r = arr::flat('A', 'B', array ('C', 'D'), 'E', 'F');
var_export( $r );
array (
  0 => 'A',
  1 => 'B',
  2 => 'C',
  3 => 'D',
  4 => 'E',
  5 => 'F',
)

PLZ: A handy PHP library for lazy programmers API documentation generated by ApiGen