Simple & secure helper to manipulate arrays in various ways

Overview

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]_!@#$&%*^' => '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)

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]_!@#$&%*^' => '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)

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]_!@#$&%*^' => '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)

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/

You might also like...
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

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

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

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 =

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

🏆 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

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

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

Safely break down arrays or objects, and put them back together in new shapes.

traverse/reshape traverse() and reshape() are companion functions that safely break down arrays or objects and put them back together in new shapes. t

PHP functions that help you validate structure of complex nested PHP arrays.

PHP functions that help you validate structure of complex nested PHP arrays.

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

: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

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

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

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

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

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

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

Parse DSN strings into value objects to make them easier to use, pass around and manipulate

DSN parser Parse DSN strings into value objects to make them easier to use, pass around and manipulate. Install Via Composer composer require nyholm/d

Releases(v1.0.9)
Owner
Gurukami
Gurukami
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.

Wojciech Mleczek 13 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

Friends Of REDAXO 17 Sep 12, 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.

Bagisto 2 May 31, 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

Bruno Meilick 13 Nov 9, 2022
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

Cossack Labs 1.6k Jan 6, 2023
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

null 10 Nov 28, 2022
Dobren Dragojević 6 Jun 11, 2023
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

Artem 4 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

GRIFART 5 Nov 11, 2022
A simple PHP library to parse and manipulate URLs

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

The League of Extraordinary Packages 351 Dec 30, 2022