Simple & secure helper to manipulate arrays in various ways

Last update: Apr 22, 2020

Array helper

Latest Stable Version Total Downloads License Build Status

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

Usage

Namespace Gurukami\Helpers

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

Arrays::exists($key, $array)

<?php

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

use \Gurukami\Helpers\Arrays;

$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name][email protected]#$&%*^' => '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][email protected]#$&%*^"]', $data); // returns: true

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

Save (Save element to the array by a string representation)

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

<?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)

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

<?php

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

use \Gurukami\Helpers\Arrays;

$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name][email protected]#$&%*^' => '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][email protected]#$&%*^"]', $data); // returns: true, delete element from array

Get (Get element of the array by a string representation)

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

<?php

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

use \Gurukami\Helpers\Arrays;

$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name][email protected]#$&%*^' => '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][email protected]#$&%*^"]', $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)

Arrays::shuffleAssoc($array)

<?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)

Null (Empty)

Search element with name 'null'

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

If you want find 'null' as constants use empty "" string

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

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

Boolean

Search element with name 'true'

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

If you want find 'true' as constants use integer 1 instead

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

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

Search element with name 'false'

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

If you want find 'false' as constants use integer 0 instead

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

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

Brackets in key name

You must surround your key name with brackets if your key name includes it or use single ' or double " quote

 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

You can get element with broken brackets in key name use single ' or double " quote

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

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.

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

You can use some styles like you want

// 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/

GitHub

https://github.com/Gurukami/php-array
You might also like...

A simple PHP library to parse and manipulate URLs

Url is a simple library to ease creating and managing Urls in PHP.

Apr 17, 2022

Validate and sanitize arrays and objects.

Aura.Filter This package provides tools to validate and sanitize objects and arrays. Foreword Installation This library requires PHP 5.4 or later; we

May 4, 2022

Make your PHP arrays sweet'n'safe

Mess We face a few problems in our PHP projects Illogical type casting (PHP's native implementation is way too "smart") Pointless casts like array =

May 23, 2022

An Hydrator class that can be used for filling object from array and extracting data from objects back to arrays.

Hydrator namespace: Meow\Hydrator Library that can hydrate (fill object with data from array) and extract data from object back to array. Installation

Feb 3, 2022

πŸ† Learn You PHP! - An introduction to PHP's core features: i/o, http, arrays, exceptions and so on.

πŸ† Learn You PHP! - An introduction to PHP's core features: i/o, http, arrays, exceptions and so on.

Learn You PHP! The very first PHP School workshop. A revolutionary new way to learn PHP Bring your imagination to life in an open learning eco-system

May 26, 2022

This package provides tools to validate and sanitize objects and arrays.

Aura.Filter This package provides tools to validate and sanitize objects and arrays. Foreword Installation This library requires PHP 7.2 or later; we

May 4, 2022

Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.

Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.

ConfigStruct Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project. It

May 2, 2022

Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js

Support I am a dad now for the last 1,5 years and that clearly shows in being on time with merging PRs or pushing this package further. Time is the bi

May 15, 2022

:date: The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects

sabre/vobject The VObject library allows you to easily parse and manipulate iCalendar and vCard objects using PHP. The goal of the VObject library is

May 15, 2022

A set of classes to create and manipulate HTML objects abstractions

HTMLObject HTMLObject is a set of classes to create and manipulate HTML objects abstractions. Static calls to the classes echo Element::p('text')-cla

Apr 22, 2022

MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way. ( WEBSITE VERSION )

MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way. ( WEBSITE VERSION )

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

Feb 14, 2022

MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

Nov 20, 2021

A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly.

PHPColors A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly. Requirements PHPColo

May 5, 2022

PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)

Gettext Note: this is the documentation of the new 5.x version. Go to 4.x branch if you're looking for the old 4.x version Created by Oscar Otero http

May 15, 2022

PHP library to detect and manipulate indentation of strings and files

indentation PHP library to detect and manipulate the indentation of files and strings Installation composer require --dev colinodell/indentation Usage

Apr 19, 2022

Simple searching for postcodes to retrieve geographic information. Support for various API providers and a unified address/output format.

Postcode Search Simple searching for postcodes to retrieve geographic information. Support for various API providers and a unified address/output form

Aug 4, 2021

A simple helper to generate and display pagination navigation links

Intro to CHocoCode Paginator Friendly PHP paginator to paginate everything This package introduces a different way of pagination handling. You can rea

Aug 24, 2021

Template for repository helper, library - Basic, Simple and Lightweight

Template start helper, library Template for repository helper, library - Basic, Simple and Lightweight Use this Template First, you can Use this templ

Sep 23, 2021

AlphaID Helper - Basic, Simple and Lightweight

AlphaID Helper - Basic, Simple and Lightweight

Oct 18, 2021
This package was created to provide simple way to manipulate arrays in PHP

PHP Collections This package was created to provide simple way to manipulate arrays in PHP. The package was inspired by the Laravel Collections.

Jul 26, 2021
YCOM Impersonate. Login as selected YCOM user πŸ§™β€β™‚οΈin frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Jan 19, 2022
The Laravel eCommerce Image Gallery allows the admin to add/manage images into various galleries and galleries into various groups according to requirements.

The Laravel eCommerce Image Gallery allows the admin to add/manage images into various galleries and galleries into various groups according to requirements.

Jan 14, 2022
Helper to automatically load various Kirby extensions in a plugin

Autoloader for Kirby Helper to automatically load various Kirby extensions in a plugin Commerical Usage This package is free but if you use it in a co

Apr 20, 2022
Simple libary for functional programing paradigm with arrays.
Simple libary for functional programing paradigm with arrays.

rodrigodornelles/php-array-lib simple libary for functional programing paradigm with arrays Features Test driven development style (TDD) PHP version c

Apr 17, 2022
A helper package to flash a bootstrap alert to the browser via a Facade or a helper function.

Alert Box (Laravel) A helper package to flash a bootstrap alert to the browser via a Facade or a helper function. <div class="alert alert-info fade in

Jan 25, 2022
Cache-purge-helper - Additional instances where nginx-helper and lscache plugin should be purged.

cache-purge-helper Additional instances where nginx-helper and lscache plugin should be purged. Install Extract the zip file. Upload them to /wp-conte

Mar 3, 2022
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

May 18, 2022
These are simple array and object collections that provide convinient methods to manipulate them.

Simple Collections These are simple array and object collections that provide convinient methods to manipulate collections; To install this package ty

Nov 19, 2021
A simple library to access and manipulate database records. Built on top of Dibi and hardwired for PostgreSQL.

grifart/tables A simple library to access and manipulate database records. Built on top of Dibi and hardwired for PostgreSQL. This library is develope

Apr 18, 2022