waUtils

Various helpers

Contents...

Methods

public static function arrayInsertAfter (array $array, $key, array $new)

Adds new array items after a specified key.

Parameters

Example

$array = array(
    'name' => 'value',
    'name2' => 'value2',
    'name3' => 'value3',
    'name4' => 'value4',
);

waUtils::arrayInsertAfter($array, 'name2', array('name5' => 'value5'));

Result

array(
    'name' => 'value',
    'name2' => 'value2',
    'name5' => 'value5',
    'name3' => 'value3',
    'name4' => 'value4',
);

public static function dropNotPositive ($int_array)

Filters out array items which are not positive numbers.

Parameters

Example

$array = array(
    -2,
    -1,
    0,
    1,
    2,
);

Result

array(
    3 => 1,
    4 => 2,
);

public static function extractValuesByKeys (array $array, $keys = array(), $skip = true, $populate = null)

Returns array items with specified keys.

Parameters

Example

$array = array(
    'apple' => 100,
    'orange' => 200,
    'pineapple' => 300,
    'watermelon' => 400,
);

$keys = array(
    'orange',
    'watermelon',
    'strawberry',
);

waUtils::extractValuesByKeys($array, $keys);

Result

array(
    'orange' => 200,
    'watermelon' => 400,
);

Example

$array = array(
    'apple' => 100,
    'orange' => 200,
    'pineapple' => 300,
    'watermelon' => 400,
);

$keys = array(
    'orange',
    'watermelon',
    'strawberry',
);

waUtils::extractValuesByKeys($array, $keys, false, 0);

Result

array(
    'orange' => 200,
    'watermelon' => 400,
    'strawberry' => 0,
);

public static function getFieldValues (array $array, $field, $index_key = null)

Extracts the values of a field from array items, associative sub-arrays or objects.

Parameters

Example

$array = array(
	111 => array(
		'id' => 1,
		'name' => 'one',
	),
	222 => array(
		'id' => 2,
		'name' => 'two',
	),
	333 => array(
		'id' => 3,
		'name' => 'three',
	),
);
waUtils::getFieldValues($array, 'name', 'id');
waUtils::getFieldValues($array, 'name', true);
waUtils::getFieldValues($array, 'name', null);

Result

//key is 'id'
array(
    1 => 'one',
    2 => 'two',
    3 => 'three',
);

//true
array(
    111 => 'one',
    222 => 'two',
    333 => 'three',
);

//null
array(
    0 => 'one',
    1 => 'two',
    2 => 'three',
);

public static function groupBy (array $array, $field, $type = 'collect')

Groups sub-arrays by specified field’s values. Those values are used as the keys of the resulting array.

Parameters

Example

$array = array(
    'key1' => array(
        'name' => 'Name 1',
        'value' => 111,
    ),
    'key2' => array(
        'name' => 'Name 1',
        'value' => 222,
    ),
    'key3' => array(
        'name' => 'Name 2',
        'value' => 333,
    ),
    'key4' => array(
        'name' => 'Name 2',
        'value' => 444,
    ),
    'key5' => array(
        'name' => 'Name 3',
        'value' => 555,
    ),
    'key6' => array(
        'name' => 'Name 3',
        'value' => 666,
    ),
);

waUtils::groupBy($array, 'name', 'collect');
waUtils::groupBy($array, 'name', 'first');
waUtils::groupBy($array, 'name', 'last');

Result

//'collect'
array(
    'Name 1' => array(
        'key1' => array(
            'name' => 'Name 1',
            'value' => 111,
        ),
        'key2' => array(
            'name' => 'Name 1',
            'value' => 222,
        ),
    ),
    'Name 2' => array(
        'key3' => array(
            'name' => 'Name 2',
            'value' => 333,
        ),
        'key4' => array(
            'name' => 'Name 2',
            'value' => 444,
        ),
    ),
    'Name 3' => array(
        'key5' => array(
            'name' => 'Name 3',
            'value' => 555,
        ),
        'key6' => array(
            'name' => 'Name 3',
            'value' => 666,
        ),
    ),
);

//'first'
array(
    'Name 1' => array(
        'name' => 'Name 1',
        'value' => 111,
    ),
    'Name 2' => array(
        'name' => 'Name 2',
        'value' => 333,
    ),
    'Name 3' => array(
        'name' => 'Name 3',
        'value' => 555,
    ),
);

//'last'
array(
    'Name 1' => array(
        'name' => 'Name 1',
        'value' => 222,
    ),
    'Name 2' => array(
        'name' => 'Name 2',
        'value' => 444,
    ),
    'Name 3' => array(
        'name' => 'Name 3',
        'value' => 666,
    ),
);

public static function jsonDecode ($json, $assoc = false, $depth = 512, $options = 0)

Wrapper for json_decode() function. In case of a failure while parsing a JSON string, the method generates a waException class exception with an error message.

Parameters

Example

$json = '{
    "name": "value",
    "name2": "value2",
    "name3": "value3",
    "name4": "value4"
}';

waUtils::jsonDecode($json, true);

Result

array(
    'name' => 'value',
    'name2' => 'value2',
    'name3' => 'value3',
    'name4' => 'value4',
);

public static function jsonEncode ($value, $options = 0, $depth = 512)

Wrapper for json_encode() function.

Parameters

Example

$array = array(
    'name' => 'value',
    'name2' => 'value2',
    'name3' => 'value3',
    'name4' => 'value4',
);

Result

// debug mode is enabled
'{
    "name": "value",
    "name2": "value2",
    "name3": "value3",
    "name4": "value4"
}'

public static function orderKeys (array $array, $order = array())

Sorts items of an associative array in the specified order.

Parameters

Example

$array = array(
    'apple' => 100,
    'orange' => 200,
    'pineapple' => 300,
    'watermelon' => 400,
);

$order = array(
    'watermelon',
    'pineapple',
    'strawberry',
);

waUtils::orderKeys($array, $order;

Result

array(
    'watermelon' => 400,
    'pineapple' => 300,
    'apple' => 100,
    'orange' => 200,
);

public static function toIntArray ($val)

Returns an array of values cast to integer type.

Parameters

Example

$array = array(
    'foo',
    1,
    '2',
    null,
    array(),
    array(1, 2, 3)
);

waUtils::toIntArray($array)

Result

array(
    0 => 0,
    1 => 1,
    2 => 2,
    3 => 0,
    4 => 0,
    5 => 0,
);

public static function toStrArray ($val, $trim = true)

Returns an array of values cast to string type.

Parameters

Example

$array = array(
    'foo',
    ' bar ',
    null,
    ' 1 ',
);

waUtils::toStrArray($array);

Result

array(
    0 => 'foo',
    1 => 'bar',
    2 => '',
    3 => '1',
);

Example

$array = array(
    'foo',
    ' bar ',
    null,
    ' 1 ',
);

waUtils::toStrArray($array, false);

Result

array(
    0 => 'foo',
    1 => ' bar ',
    2 => '',
    3 => ' 1 ',
);

public static function varExportToFile ($var, $file, $export = true)

Saves a variable value to a configuration file.

Parameters

Example

//array
$array_var = array(
    'id' => 'some',
    'url' => '/test/',
);
$file = wa()->getConfig()->getConfigPath('plugins/test/config_array.php');
waUtils::varExportToFile($var, $file);

//scalar value
$scalar_var = 1528117809;
$file = wa()->getConfig()->getConfigPath('plugins/test/config_scalar.php');
waUtils::varExportToFile($scalar_var, $file, false);