PHP code example of chr15k / array

1. Go to this page and download the library: Download chr15k/array library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chr15k / array example snippets




use Chr15k\Arr\Arr;

$isAccessible = Arr::accessible(['a' => 1, 'b' => 2]);

// true

$isAccessible = Arr::accessible('abc');

// false

$isAccessible = Arr::accessible(new stdClass);

// false



use Chr15k\Arr\Arr;

$array = Arr::add(['name' => 'Desk'], 'price', 100);

// ['name' => 'Desk', 'price' => 100]

$array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);

// ['name' => 'Desk', 'price' => 100]



use Chr15k\Arr\Arr;

$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

// [1, 2, 3, 4, 5, 6, 7, 8, 9]



use Chr15k\Arr\Arr;

$matrix = Arr::crossJoin([1, 2], ['a', 'b']);

/*
    [
        [1, 'a'],
        [1, 'b'],
        [2, 'a'],
        [2, 'b'],
    ]
*/

$matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']);

/*
    [
        [1, 'a', 'I'],
        [1, 'a', 'II'],
        [1, 'b', 'I'],
        [1, 'b', 'II'],
        [2, 'a', 'I'],
        [2, 'a', 'II'],
        [2, 'b', 'I'],
        [2, 'b', 'II'],
    ]
*/



use Chr15k\Arr\Arr;

[$keys, $values] = Arr::divide(['name' => 'Desk']);

// $keys: ['name']

// $values: ['Desk']



use Chr15k\Arr\Arr;

$array = ['products' => ['desk' => ['price' => 100]]];

$flattened = Arr::dot($array);

// ['products.desk.price' => 100]



use Chr15k\Arr\Arr;

$array = ['name' => 'Desk', 'price' => 100];

$filtered = Arr::except($array, ['price']);

// ['name' => 'Desk']



use Chr15k\Arr\Arr;

$array = ['name' => 'John Doe', 'age' => 17];

$exists = Arr::exists($array, 'name');

// true

$exists = Arr::exists($array, 'salary');

// false



use Chr15k\Arr\Arr;

$array = [100, 200, 300];

$first = Arr::first($array, function ($value, $key) {
    return $value >= 150;
});

// 200


// A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test:

$first = Arr::first($array, $callback, $default);



use Chr15k\Arr\Arr;

$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];

$flattened = Arr::flatten($array);

// ['Joe', 'PHP', 'Ruby']



use Chr15k\Arr\Arr;

$array = ['products' => ['desk' => ['price' => 100]]];

Arr::forget($array, 'products.desk');

// ['products' => []]



use Chr15k\Arr\Arr;

$array = ['products' => ['desk' => ['price' => 100]]];

$price = Arr::get($array, 'products.desk.price');

// 100


// The Arr::get method also accepts a default value, which will be returned if the specific key is not found:

$discount = Arr::get($array, 'products.desk.discount', 0);

// 0



use Chr15k\Arr\Arr;

$array = ['product' => ['name' => 'Desk', 'price' => 100]];

$contains = Arr::has($array, 'product.name');

// true

$contains = Arr::has($array, ['product.price', 'product.discount']);

// false



use Chr15k\Arr\Arr;

$array = ['product' => ['name' => 'Desk', 'price' => 100]];

$contains = Arr::hasAny($array, 'product.name');

// true

$contains = Arr::hasAny($array, ['product.name', 'product.discount']);

// true

$contains = Arr::hasAny($array, ['category', 'product.discount']);

// false



use Chr15k\Arr\Arr;

$isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]);

// true

$isAssoc = Arr::isAssoc([1, 2, 3]);

// false



use Chr15k\Arr\Arr;

$isMultiDimensional = Arr::isMultiDimensional(['product' => ['name' => 'Desk', 'price' => 100]]);

// true

$isMultiDimensional = Arr::isMultiDimensional([2, 3, [4]]);

// true

$isMultiDimensional = Arr::isMultiDimensional([2, 3, 4]);

// false

$isMultiDimensional = Arr::isMultiDimensional(['name' => 'Desk', 'price' => 100]);

// false


// Accounts for empty arrays

$isMultiDimensional = Arr::isMultiDimensional(['name' => 'Desk', 'price' => 100, []]);

// true



use Chr15k\Arr\Arr;

$array = [100, 200, 300, 110];

$last = Arr::last($array, function ($value, $key) {
    return $value >= 150;
});

// 300

// A default value may be passed as the third argument to the method. This value will be returned if no value passes the truth test:

$last = Arr::last($array, $callback, $default);



use Chr15k\Arr\Arr;

$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

$slice = Arr::only($array, ['name', 'price']);

// ['name' => 'Desk', 'price' => 100]



use Chr15k\Arr\Arr;

$array = [
    ['developer' => ['id' => 1, 'name' => 'Taylor']],
    ['developer' => ['id' => 2, 'name' => 'Abigail']],
];

$names = Arr::pluck($array, 'developer.name');

// ['Taylor', 'Abigail']

// You may also specify how you wish the resulting list to be keyed:

$names = Arr::pluck($array, 'developer.name', 'developer.id');

// [1 => 'Taylor', 2 => 'Abigail']



use Chr15k\Arr\Arr;

$array = ['one', 'two', 'three', 'four'];

$array = Arr::prepend($array, 'zero');

// ['zero', 'one', 'two', 'three', 'four']


// If needed, you may specify the key that should be used for the value:

$array = ['price' => 100];

$array = Arr::prepend($array, 'Desk', 'name');

// ['name' => 'Desk', 'price' => 100]



use Chr15k\Arr\Arr;

$array = ['name' => 'Desk', 'price' => 100];

$name = Arr::pull($array, 'name');

// $name: Desk

// $array: ['price' => 100]


// A default value may be passed as the third argument to the method. This value will be returned if the key doesn't exist:

$value = Arr::pull($array, $key, $default);



use Chr15k\Arr\Arr;

$array = ['name' => 'Taylor', 'order' => ['column' => 'created_at', 'direction' => 'desc']];

Arr::query($array);

// name=Taylor&order[column]=created_at&order[direction]=desc



use Chr15k\Arr\Arr;

$array = [1, 2, 3, 4, 5];

$random = Arr::random($array);

// 4 - (retrieved randomly)


// You may also specify the number of items to return as an optional second argument.
// Note that providing this argument will return an array, even if only one item is desired:

$items = Arr::random($array, 2);

// [2, 5] - (retrieved randomly)



use Chr15k\Arr\Arr;

$array = ['products' => ['desk' => ['price' => 100]]];

Arr::set($array, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]



use Chr15k\Arr\Arr;

$array = Arr::shuffle([1, 2, 3, 4, 5]);

// [3, 2, 5, 1, 4] - (generated randomly)



use Chr15k\Arr\Arr;

$array = ['Desk', 'Table', 'Chair'];

$sorted = Arr::sort($array);

// ['Chair', 'Desk', 'Table']


// Reverse the order by passing true to the second argument

$sorted = Arr::sort($array, true);

// ['Table', 'Desk', 'Chair']



use Chr15k\Arr\Arr;

$array = [
    ['Roman', 'Taylor', 'Li'],
    ['PHP', 'Ruby', 'JavaScript'],
    ['one' => 1, 'two' => 2, 'three' => 3],
];

$sorted = Arr::sortRecursive($array);

/*
    [
        ['JavaScript', 'PHP', 'Ruby'],
        ['one' => 1, 'three' => 3, 'two' => 2],
        ['Li', 'Roman', 'Taylor'],
    ]
*/



use Chr15k\Arr\Arr;

$array = [100, '200', 300, '400', 500];

$filtered = Arr::where($array, function ($value, $key) {
    return is_string($value);
});

// [1 => '200', 3 => '400']



use Chr15k\Arr\Arr;

$string = 'Laravel';

$array = Arr::wrap($string);

// ['Laravel']

// If the given value is null, an empty array will be returned:

$nothing = null;

$array = Arr::wrap($nothing);

// []