2017 © Pedro Peláez
 

library php-array

Simple & secure helper to manipulate arrays in various ways

image

gurukami/php-array

Simple & secure helper to manipulate arrays in various ways

  • Saturday, October 8, 2016
  • by Nerufa
  • Repository
  • 9 Watchers
  • 29 Stars
  • 288 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 11 Versions
  • 2 % Grown

The README.md

Array helper

Latest Stable Version Total Downloads License Build Status, (*1)

Simple & secure helper to manipulate arrays in various ways, especially for multidimensional arrays
Forget about checking for existing keys and E_NOTICE, (*2)

Usage

Namespace Gurukami\Helpers, (*3)

Exists (Checks if the given key exists in the array by a string representation), (*4)

Arrays::exists($key, $array), (*5)

<?php

// Don't forget require 'autoload.php' composer

use \Gurukami\Helpers\Arrays;

$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name]_!@#$&%*^' => 'complex',
    'k2' => 'string'
];

Arrays::exists('k0', $data); // returns: true
Arrays::exists('k9', $data); // returns: false

Arrays::exists('[k1][k1-1]', $data); // returns: true
Arrays::exists('[k1][k1-2]', $data); // returns: false

Arrays::exists('["complex_[name]_!@#$&%*^"]', $data); // returns: true

Arrays::exists('[k2][2]', $data); // returns: false

Save (Save element to the array by a string representation), (*6)

Arrays::save($key, &$array, $value, $replace = true), (*7)

<?php

// Don't forget require 'autoload.php' composer

use \Gurukami\Helpers\Arrays;

$data = [
    'k2' => 'string'
];

Arrays::save('k0', $data, 'v0'); // returns: true, save as 'k0' => 'v0'
Arrays::save('[k1][k1-1]', $data, 'v1-1'); // returns: true, save as 'k1' => ['k1-1' => 'v1-1']
Arrays::save('[k2][2]', $data, 'p'); // returns: false, can't save value to string

// Broken key names
Arrays::save('k3[', $data, 'v3'); // returns: false, can't save, bad syntax
Arrays::save('["k4["]', $data, 'v4'); // returns: true, save as 'k4[' => 'v4'
Arrays::save('"k4["', $data, 'v4'); // returns: false, can't save, bad syntax

// Append
Arrays::save('k5', $data, []); // returns: true, create array 'k5' => []
Arrays::save('k5[]', $data, 'v5-0'); // returns: true, append value to exists array 'k5' => [ 'v5-0' ]
Arrays::save('k6[k6-1][]', $data, 'v6-1-0'); // returns: true, save as 'k6' => [ 'k6-1' => [ 'v6-1-0' ] ]

// Replace if not exists
Arrays::save('k2', $data, 'something', false); // returns false, value not replaced because value is exists

Delete (Delete element from the array by a string representation), (*8)

Arrays::delete($key, &$array), (*9)

<?php

// Don't forget require 'autoload.php' composer

use \Gurukami\Helpers\Arrays;

$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name]_!@#$&%*^' => 'complex'
];

Arrays::delete('k0', $data); // returns: true, delete element from array
Arrays::delete('k9', $data); // returns: false

Arrays::delete('[k1][k1-1]', $data); // returns: true, delete element from array
Arrays::delete('[k1][k1-2]', $data); // returns: false

Arrays::delete('["complex_[name]_!@#$&%*^"]', $data); // returns: true, delete element from array

Get (Get element of the array by a string representation), (*10)

Arrays::get($key, $array, $default = null, $ignoreString = true), (*11)

<?php

// Don't forget require 'autoload.php' composer

use \Gurukami\Helpers\Arrays;

$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name]_!@#$&%*^' => 'complex',
    'k2' => 'string'
];

Arrays::get('k0', $data); // returns: 'v0'
Arrays::get('k9', $data, '0'); // returns: '0', key isn't exists in array

Arrays::get('[k1][k1-1]', $data); // returns: 'v1-1'
Arrays::get('[k1][k1-2]', $data, 'default'); // returns: 'default', key isn't exists in array

Arrays::get('["complex_[name]_!@#$&%*^"]', $data); // returns: 'complex'

Arrays::get('[k2][2]', $data); // returns: null, key isn't exists in array

// If you want get a symbol from string value, you may switch off option $ignoreString = false
Arrays::get('[k2][2]', $data, null, false); // returns: 'r'
Arrays::get('[k2][null]', $data, null, false); // returns: null, offset isn't exists in string

Shuffle Assoc (Shuffle the array with preserved keys), (*12)

Arrays::shuffleAssoc($array), (*13)

<?php

// Don't forget require 'autoload.php' composer

use \Gurukami\Helpers\Arrays;

$data = [
    'k0' => 'v0',
    'k1' => 'v1',
    'k2' => 'v2'
];

Arrays::shuffleAssoc($data); // returns something like: ['k2' => 'v2', 'k1' => 'v1', 'k0' => 'v0']

Behaviors

{method} - any method from class (exists,save,delete,get), (*14)

Null (Empty), (*15)

Search element with name 'null', (*16)

Arrays::{method}('null', ...) // or
Arrays::{method}('[null]', ...)

If you want find 'null' as constants use empty "" string, (*17)

Arrays::{method}('', ...) // or
Arrays::{method}('[""]', ...) // or
Arrays::{method}('[key][""]', ...)

Warning! You can get element with 'null' constant only for one-dimensional array, if you need search in deeper use instructions above, (*18)

Arrays::{method}(null, ...)

Boolean, (*19)

Search element with name 'true', (*20)

Arrays::{method}('true', ...) // or
Arrays::{method}('[true]', ...)

If you want find 'true' as constants use integer 1 instead, (*21)

Arrays::{method}(1, ...) // or
Arrays::{method}('1', ...) // or
Arrays::{method}('[1]', ...) // or
Arrays::{method}('[key][1]', ...)

Warning! You can get element with 'true' constant only for one-dimensional array, if you need search in deeper use instructions above, (*22)

Arrays::{method}(true, ...)

Search element with name 'false', (*23)

Arrays::{method}('false', ...) // or
Arrays::{method}('[false]', ...)

If you want find 'false' as constants use integer 0 instead, (*24)

Arrays::{method}(0, ...) // or
Arrays::{method}('0', ...) // or
Arrays::{method}('[0]', ...) // or
Arrays::{method}('[key][0]', ...)

Warning! You can get element with 'false' constant only for one-dimensional array, if you need search in deeper use instructions above, (*25)

Arrays::{method}(false, ...)

Brackets in key name, (*26)

You must surround your key name with brackets if your key name includes it or use single ' or double " quote, (*27)

 Arrays::{method}('key[]', ...) // Wrong for all methods except save, because [] is append instruction
 Arrays::{method}('[key[]]', ...) // Works fine
 Arrays::{method}('["key[]"]', ...) // Works fine

Broken brackets in key name, (*28)

You can get element with broken brackets in key name use single ' or double " quote, (*29)

Arrays::{method}('key[', ...) // Wrong
Arrays::{method}('key[subKey[]', ...) // Wrong
Arrays::{method}('["key["]', ...) // Works fine
Arrays::{method}('key["subKey["]', ...) // Works fine

Broken quotes on boundary in key name, (*30)

If you use different quotes on boundary of key name, it will be recognized as a key name with quotes. Also key name will be recognized as a key if use quote without brackets., (*31)

Arrays::{method}('"key\'', ...) // recognized as "key'
Arrays::{method}('"key"', ...) // recognized as "key"
Arrays::{method}('["key"]', ...) // recognized as key
Arrays::{method}('[\'key\']', ...) // recognized as key

Style representation, (*32)

You can use some styles like you want, (*33)

// Normal:
Arrays::{method}('[key][subKey][subSubKey][subSubSubKey]', ...)

// Camel:
Arrays::{method}('key[subKey]subSubKey[subSubSubKey]', ...)

// HTML:
Arrays::{method}('key[subKey][subSubKey][subSubSubKey]', ...)

Requirements

  • PHP 5.4 or greater

Installation

php composer.phar require "gurukami/php-array:*"

License

The MIT license
Copyright (c) 2016 Gurukami, http://gurukami.com/, (*34)

The Versions

08/10 2016

dev-master

9999999-dev http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

29/09 2016

v1.0.9

1.0.9.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

28/09 2016

v1.0.8

1.0.8.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

24/09 2016

v1.0.7

1.0.7.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

19/09 2016

v1.0.6

1.0.6.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

18/09 2016

v1.0.5

1.0.5.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

16/09 2016

v1.0.4

1.0.4.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

15/09 2016

v1.0.3

1.0.3.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

15/09 2016

v1.0.2

1.0.2.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

15/09 2016

v1.0.1

1.0.1.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami

15/09 2016

v1.0.0

1.0.0.0 http://gurukami.com

Simple & secure helper to manipulate arrays in various ways

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Ilya Krasheninnikov

helpers php array gurukami