PHP Classes

PHP Array Functions: Determine the type and run other array operations

Recommend this page to a friend!
  Info   View files Example   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 224 This week: 1All time: 8,220 This week: 560Up
Version License PHP version Categories
php-arrays 1.0.0MIT/X Consortium ...5PHP 5, Data types
Description 

Author

This class determine the type and run other array operations.

It provides static functions that can determine if a value is really an array, it is an associative array, has multiple dimensions, has its values as a sequence of values that is sorted by those values. The class can also:

- Add new elements to the array
- Get the value of an array element
- Convert a multi-dimensional array into an associative array
- Get a list of unique values in the array
- Get a subset of the array elements
- Remove array elements with a given value
- Change the case of the array keys
- Remove entries that have duplicated values
- Get the values that appear more or less in the array
- Build a HTTP query string from the array entries
- Filter the array values using a callback function
- Shuffle the array to change the order of the elements
- Pick a random value from the array
- Get the values of a key of a multidimensional array

Innovation Award
PHP Programming Innovation award nominee
May 2019
Number 5
PHP provides an extensive set of functions to manipulate arrays. However there are applications that need to implement additional functionality not available as a built-in function in PHP.

This package provides that kind of functionality to manipulate arrays like converting multi-dimensional arrays into associatve arrays, changing the case of array keys, remove entries that have duplicated values, etc..

Manuel Lemos
Picture of Muhammad Umer Farooq
Name: Muhammad Umer Farooq is available for providing paid consulting. Contact Muhammad Umer Farooq .
Classes: 52 packages by
Country: Pakistan Pakistan
Age: 22
All time rank: 84611 in Pakistan Pakistan
Week rank: 52 Up3 in Pakistan Pakistan Up
Innovation award
Innovation award
Nominee: 6x

Example

<?php

use Lablnet\Arrays;
require
'src/Arrays.php';

var_dump(new Arrays());


Details

A class to manipulate arrays in efficient ways to solve real world problems.

Requirement

  1. PHP 7.2 or grater.
  2. Composer.

Installation

Installing this package is very simple, first ensure you have the right PHP version and composer installed then in your terminal/(command prompt) run: composer require lablnet/arrays

Features

1. Determine whether the given dataSet is really array?

- Example:   
- ```php
    use Lablnet\Arrays;
    require '../vendor/autoload.php';
    $arr = array(2, 2, 3, 4, 4, 4, 4, 8, 8, 6, 6, 9, 9, 9, 9); 
    var_dump(Arrays::isReallyArray($arr)); //True
    var_dump(Arrays::isReallyArray([]));   //False
    ```

2. Determine the given array is (sequential)indexes.?

- Example:   
- ```php
    use Lablnet\Arrays;
    require '../vendor/autoload.php';
    $arr = array(2, 2, 3, 4, 4, 4, 4, 8, 8, 6, 6, 9, 9, 9, 9); 
    var_dump(Arrays::isSequential($arr)); //True
    var_dump(Arrays::isSequential(['a' => 1]));   //False
    ```

3. Determine the given array is assoc.?

- Example:   
- ```php
    use Lablnet\Arrays;
    require '../vendor/autoload.php';
    $arr = array(2, 2, 3, 4, 4, 4, 4, 8, 8, 6, 6, 9, 9, 9, 9); 
    var_dump(Arrays::isAssoc($arr)); //False
    var_dump(Arrays::isAssoc(['a' => 1]));   //True
    ```

4. Determine the given array is multi-dimensional.?

- Example:   
- ```php
    use Lablnet\Arrays;
    require '../vendor/autoload.php';
    $arr = array(2, 2, 3, 4, 4, 4, 4, 8, 8, 6, 6, 9, 9, 9, 9); 
    var_dump(Arrays::isMulti($arr)); //False
    var_dump(Arrays::isMulti(['a' => ['b' => 'c']]));   //True
    ```

5. Get the type of array.

- Example:   
- ```php
    use Lablnet\Arrays;
    require '../vendor/autoload.php';
    $arr = array(2, 2, 3, 4, 4, 4, 4, 8, 8, 6, 6, 9, 9, 9, 9); 
    var_dump(Arrays::getType($arr)); //indexes
    var_dump(Arrays::getType(['a' => ['b' => 'c']]));   //multi
    ```

  1. Add an element to an array using "operation" notation if it doesn't exist. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; var_dump(Arrays::add(['name' => 'desk', 'price' => null], 'price', 100)); // ['name' => 'desk', 'price' => 100] ```

    7. Set an array item to a given value using "operator" notation. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['products' => ['desk' => ['price' => 100]]]; Arrays::set($array, 'products.desk.price', 200, '.'); // ['products' => ['desk' => ['price' => 200]]] ```

  2. Get an item from an array using "operator" notation(The `Arrays::get` method retrieves a value from a deeply nested array using "dot" notation:). - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['products' => ['desk' => ['price' => 100]]]; $price = Arrays::get($array, 'products.desk.price', '.'); // 100 ``` The `Arrays::get` method also accepts a default value, which will be returned if the specific key is not found.

    9. Determine if an item or items exist in an array using 'Operator' notation.
    - Example:
    - ```php
        use Lablnet\Arrays;
        require '../vendor/autoload.php';
    		$array = ['product' => ['name' => 'Desk', 'price' => 100]];
    		$contains = Arrays::has($array, 'product.name', '.');
    		// true
    		$contains = Arrays::has($array, ['product.price', 'product.discount'], '.');
    		// false
    
    
  3. Determine if an item or items exist in an array using 'Operator' notation. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['product' => ['name' => 'Desk', 'price' => 100]]; $assoc = Arrays::multiToAssoc($array); // ['name' => 'Desk', 'price' => 100]
  4. Converted a multi-dimensional associative array with `dot`.
    - Example:
    - ```php
        use Lablnet\Arrays;
        require '../vendor/autoload.php';
    		$array = ['products' => ['desk' => ['price' => 100]]];
    		$dot= Arrays::dot($array);
    		// ['products.desk.price' => 100]
    
    
  5. Converted a multi-dimensional associative array with `operator`. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['products' => ['desk' => ['price' => 100]]]; $dot = Arrays::multiToAssocWithSpecificOpr($array); // ['products.desk.price' => 100]
  6. Push an item onto the beginning of an array.
    - Example:
    - ```php
        use Lablnet\Arrays;
        require '../vendor/autoload.php';
        $array = ['red', 'green', 'blue'];
        $prepend = Arrays::prepend($array, 'yellow');
        // ['yellow', 'red', 'green', 'blue'];
    
    
  7. Push an item to the end of array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['red', 'green', 'blue']; $append = Arrays::append($array, 'yellow'); // ['red', 'green', 'blue', 'yellow'];
  8. Get the unique elements from arrays.
    - Example:
    - ```php
        use Lablnet\Arrays;
        require '../vendor/autoload.php';
        $array = ['red', 'green', 'blue', 'red'];
        $unique = Arrays::append($array);
        // ['red', green', 'blue'];
    
    
  9. Get a subset of the items from the given array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = [ 'users' => [ 'id' => 1, 'name' => "Alex", 'username' => 'peter', ], [ 'id' => 2, 'name' => "Peter Khot", 'username' => 'peter', ], [ 'id' => 3, 'name' => "John", 'username' => 'test', ] ]; $subSetOfArray = Arrays::subSetOfArray($array, 'name'); // [];
  10. Remove one or many array items from a given array using "operator" notation.
    - Example:
    - ```php
        use Lablnet\Arrays;
        require '../vendor/autoload.php';
    		$array = ['products' => ['desk' => ['price' => 100]]];
    		Arrays::forget($array, 'products.desk');
    		// ['products' => []]
    
    
  11. Get all of the given array except for a specified array of keys. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['name' => 'Desk', 'price' => 100] $filtered = Arrays::except($array, ['price']); // ['name' => 'Desk']
  12. Get a value from the array, and remove it.
     - Example:
     - ```php
        use Lablnet\Arrays;
        require '../vendor/autoload.php';
    		$array = ['name' => 'Desk', 'price' => 100];
    		$name = Arrays::pull($array, 'name');
    		// $array: ['price' => 100]
    
    
  13. Changes the case of all keys in an array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['name' => 'Desk', 'price' => 100]; $name = Arrays::arrayChangeCaseKey($array, CASE_UPPER); // ['NAME' => 'Desk', 'PRICE' => 100] ``` 21. Changes the case of all values in an array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['name' => 'Alex']; $name = Arrays::arrayChangeCaseValue($array, CASE_UPPER); // ['name' => 'ALEX'] ```
  14. Remove duplicate values from array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ```` $name = Arrays::removeDuplicates($array); // $array: ['red', 'green', 'blue'] ```
  15. Get the most occurring value from array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['red', 'blue', 'green', 'red', 'blue']; $name = Arrays::mostOccurring($array); // ['red', 'blue'] ```
  16. Get the least occurring value from array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['red', 'blue', 'green', 'red', 'blue']; $name = Arrays::leastOccurring($array); // ['green'] ```
  17. Convert the array into a query string. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = [ 'foo' => 'bar', 'baz' => 'boom', 'cow' => 'milk', 'php' => 'hypertext processor' ]; $name = Arrays::query($array); // foo=bar&baz=boom&cow=milk&php=hypertext+processor ```
  18. Filter the array using the given callback (THIS METION WILL NOT WORKS WITH MULTIDIMESSIONAL ARRAY.). - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = [100, '200', 300, '400', 500]; $array = Arrays::where($array, function ($value, $key) { return is_string($value); }); // [1 => '200', 3 => '400'] ```
  19. Get one or a specified number of random values from an array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = ['red', 'green', 'blue']; $name = Arrays::random($array); // ['blue']; ``` 28. Get multiple values of same keys from multi-dimessional array. - Example: - ```php use Lablnet\Arrays; require '../vendor/autoload.php'; $array = [ ['developer' => ['id' => 1, 'name' => 'Alex']], ['developer' => ['id' => 2, 'name' => 'Peter']], ]; $name = Arrays::pluck($array, 'name'); // ['Alex', 'Peter']; ```

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imageTests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation
Accessible without login Plain text file test.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file Arrays.php Class Class source

  Files folder image Files  /  Tests  
File Role Description
  Plain text file ArraysTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:224
This week:1
All time:8,220
This week:560Up