PHP code example of tfhinc / ci-ray
1. Go to this page and download the library: Download tfhinc/ci-ray 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/ */
tfhinc / ci-ray example snippets
// Given an array...
$fruit = [
'lemon' => 'yellow',
'apple' => 'red',
'lime' => 'green',
'pear' => 'green',
];
// ...Get all of the keys except 'apple' and 'lime':
ray($fruit)->except(['apple', 'lime'])->toArray();
/*
[
'lemon' => 'yellow',
'pear' => 'green',
]
*/
// ...Get the first value:
ray($fruit)->first();
/*
'yellow'
*/
// ...Sort by value:
ray($fruit)->sortByValues()->toArray();
/*
[
'lime' => 'green',
'pear' => 'green',
'apple' => 'red',
'lemon' => 'yellow',
]
*/
// toArray() returns the final transformed array:
ray($fruit_multi)->column('color', 'name')->sortByKeys()->toArray();
/*
[
'apple' => 'red',
'lemon' => 'yellow',
'lime' => 'green',
'pear' => 'green',
]
*/
// count() returns an integer:
ray($fruit_multi)->whereIn('color', ['green','yellow'])->count();
/*
3
*/
// first() returns a string:
ray($fruit)->first();
/*
'yellow'
*/
ray($fruit)->sortByKeys()->toArray();
/*
Array
(
[apple] => red
[lemon] => yellow
[lime] => green
[pear] => green
)
*/
ray($fruit)->sortByValues()->toArray();
/*
Array
(
[lime] => green
[apple] => red
[lemon] => yellow
)
*/
ray($fruit_multi)->has('price');
// true
ray($fruit_multi)->has('brand');
// false
ray($fruit_multi)->contains('green');
// true
ray($fruit_multi)->contains('brown');
// false
ray($fruit_multi)->contains('color', 'green');
// true
ray($fruit_multi)->contains('color', 'brown');
// false
ray($fruit_multi)->sum('qty');
// 30
ray($fruit_multi)->sum('price');
// 8.74
ray($fruit_multi)->avg('price');
// 2.185
ray($fruit_multi)->count();
// 4
ray($fruit)->values()->toArray();
/*
Array
(
[0] => yellow
[1] => red
[2] => green
[3] => green
)
*/
ray($fruit)->first();
// yellow
ray($fruit_multi)->first();
/*
Array
(
[id] => 1
[name] => lemon
[color] => yellow
[price] => 2.25
[qty] => 2
)
*/
ray($fruit)->last();
// green
ray($fruit_multi)->last();
/*
Array
(
[id] => 4
[name] => pear
[color] => green
[price] => 2
[qty] => 7
)
*/
ray($fruit)->except(['apple', 'lime'])->toArray();
/*
Array
(
[lemon] => yellow
[pear] => green
)
*/
ray($fruit)->only(['apple', 'lime'])->toArray();
/*
Array
(
[apple] => red
[lime] => green
)
*/
ray($fruit)->unique()->toArray();
/*
Array
(
[lemon] => yellow
[apple] => red
[lime] => green
)
*/
ray($fruit_multi)->unique('color')->toArray();
/*
Array
(
[0] => Array
(
[id] => 1
[name] => lemon
[color] => yellow
[price] => 2.25
[qty] => 2
)
[1] => Array
(
[id] => 2
[name] => apple
[color] => red
[price] => 0.99
[qty] => 12
)
[2] => Array
(
[id] => 3
[name] => lime
[color] => green
[price] => 3.5
[qty] => 9
)
)
*/
ray($fruit_multi)->groupBy('color')->toArray();
/*
Array
(
[yellow] => Array
(
[0] => Array
(
[id] => 1
[name] => lemon
[color] => yellow
[price] => 2.25
[qty] => 2
)
)
[red] => Array
(
[0] => Array
(
[id] => 2
[name] => apple
[color] => red
[price] => 0.99
[qty] => 12
)
)
[green] => Array
(
[0] => Array
(
[id] => 3
[name] => lime
[color] => green
[price] => 3.5
[qty] => 9
)
[1] => Array
(
[id] => 4
[name] => pear
[color] => green
[price] => 2
[qty] => 7
)
)
)
*/
ray($fruit_multi)->column('color')->toArray();
/*
Array
(
[0] => yellow
[1] => red
[2] => green
[3] => green
)
*/
ray($fruit_multi)->column('color', 'name')->toArray();
/*
Array
(
[lemon] => yellow
[apple] => red
[lime] => green
[pear] => green
)
*/
ray($fruit_multi)->where('color', 'green')->toArray();
/*
Array
(
[2] => Array
(
[id] => 3
[name] => lime
[color] => green
[price] => 3.5
[qty] => 9
)
[3] => Array
(
[id] => 4
[name] => pear
[color] => green
[price] => 2
[qty] => 7
)
)
*/
ray($fruit_multi)->whereIn('color', ['green', 'yellow'])->toArray();
/*
Array
(
[0] => Array
(
[id] => 1
[name] => lemon
[color] => yellow
[price] => 2.25
[qty] => 2
)
[2] => Array
(
[id] => 3
[name] => lime
[color] => green
[price] => 3.5
[qty] => 9
)
[3] => Array
(
[id] => 4
[name] => pear
[color] => green
[price] => 2
[qty] => 7
)
)
*/
ray($fruit_multi)->whereNot('color', 'green')->toArray();
/*
Array
(
[0] => Array
(
[id] => 1
[name] => lemon
[color] => yellow
[price] => 2.25
[qty] => 2
)
[1] => Array
(
[id] => 2
[name] => apple
[color] => red
[price] => 0.99
[qty] => 12
)
)
*/
ray($fruit)->whereNotIn('color', ['green', 'yellow'])->toArray();
/*
Array
(
[1] => Array
(
[id] => 2
[name] => apple
[color] => red
[price] => 0.99
[qty] => 12
)
)
*/
ray($fruit_multi)->filter(function($value, $key) {
return $value['price'] > 3;
})->toArray();
/*
Array
(
[2] => Array
(
[id] => 3
[name] => lime
[color] => green
[price] => 3.5
[qty] => 9
)
)
*/
ray($fruit_multi)->filter(function($value, $key) {
return $value['price'] < 3;
})->toArray();
/*
Array
(
[0] => Array
(
[id] => 1
[name] => lemon
[color] => yellow
[price] => 2.25
[qty] => 2
)
[1] => Array
(
[id] => 2
[name] => apple
[color] => red
[price] => 0.99
[qty] => 12
)
[3] => Array
(
[id] => 4
[name] => pear
[color] => green
[price] => 2
[qty] => 7
)
)
*/
ray($fruit_multi)->reduce(function($carry, $value) {
return $value['qty'] < 3 ? $carry + $value['qty'] : $carry;
});
// 2
php
$this->load->helper('ray');
php
$ray = new TFHInc/Ray/Ray();
php
$this->load->library('Ray');