24
PHP, arrays and ๐ฅ,๐,๐,๐ฅญ,๐,๐,๐
Arr is a class for managing PHP arrays, so you can use methods to perform some common operations.
Array in PHP is a basic but powerful data structure for managing a list of elements (like fruits ...).
Arrays in PHP are list of map (pair of key => value).
Usually keys are integer that start from 0 (first element has position/index 0, the second one has position/index 1 etc...).
In this case, arrays are treated like an indexed array.
But keys could be "named" (integer, string...). In this case, arrays are treated like an associative array.
For example:
- ['๐', '๐'] : is an array with 2 elements, the first one with index 0 and the second one with index 1;
- ['apple' => '๐', 'banana' => '๐']: is an array with 2 elements with the same values as the previous one, but different indexes. In this case, the index 'apple' is related with value '๐', the index 'banana' is related with value '๐'.
We created a small open source package for managing arrays (trying to mimic some functionalities of Javascript array Object).
- The source code: https://github.com/Hi-Folks/array
- The Package: https://packagist.org/packages/hi-folks/array
- The documentation: https://github.com/Hi-Folks/array/blob/main/doc/readme.md
- The list of method already implemented and the methods that we are going to implement: https://github.com/Hi-Folks/array/blob/main/doc/readme.md
- A cheat sheet: https://github.com/Hi-Folks/array/blob/main/examples/cheatsheet.php
To install the package, you can use composer as usual:
composer require hi-folks/array
In your script, you can include autoload.php file, generated by composer (unless you are using a framework that includes autmatically autolload.php file):
require("./vendor/autoload.php");
Then you can include your class:
use HiFolks\DataType\Arr;
You can create a list of fruits (using Arr class) with static method make() and then you can access to all Arr object methods, for example length():
use HiFolks\DataType\Arr;
$fruits = Arr::make(['๐', '๐']);
echo $fruits->length();
// Output: 2
Even if you are using Arr class, you can access to the element with square brackets [] like with PHP standard array:
- $fruits[0];
- $fruits[ $fruits->length() - 1];
use HiFolks\DataType\Arr;
// Create an array
$fruits = Arr::make(['๐', '๐']);
// First element
$first = $fruits[0];
echo $first;
// ๐
echo PHP_EOL . "--~--" . PHP_EOL;
// Last element
$last = $fruits[ $fruits->length() - 1];
echo $last;
// ๐
echo PHP_EOL . "--~--" . PHP_EOL;
If you need to walk through an array, you can use forEach() method.
You can specify the function as argument of forEach() method in order to manage each single element (with key)
use HiFolks\DataType\Arr;
// Create an array
$fruits = Arr::make([
'kiwi' =>'๐ฅ',
'fragola' => '๐',
'lemon' => '๐',
'mango' => '๐ฅญ',
'apple' => '๐',
'banana' => '๐']);
// Loop over an array
$fruits->forEach(function ($element, $key) {
echo $key . " " . $element . "; ";
});
// kiwi ๐ฅ; fragola ๐; lemon ๐; mango ๐ฅญ; apple ๐; banana ๐;
Add an item to the end of an array with push() method:
use HiFolks\DataType\Arr;
// Create some fruits
$fruits = Arr::make(['๐ฅ','๐','๐','๐ฅญ','๐','๐']);
// Push a new fruit (peach)
$fruits->push('๐');
// Loop over fruits
$fruits->forEach(function ($element, $key) {
echo $key . " " . $element . PHP_EOL;
});
echo PHP_EOL . "--~--" . PHP_EOL;
Get and remove element from the end of the array with pop() method:
use HiFolks\DataType\Arr;
// Create some fruits
$fruits = Arr::make(['๐ฅ','๐','๐','๐ฅญ','๐','๐']);
// pop (retrieve and remove) last elements
$last = $fruits->pop();
$secondLast = $fruits->pop();
// Loop over fruits
$fruits->forEach(function ($element, $key) {
echo $key . " " . $element . PHP_EOL;
});
echo "Last fruit: " . $last . PHP_EOL; // banana
echo "Second last fruit: " . $secondLast . PHP_EOL; // apple
Get and remove the first element from the beginning of the array with shift() method:
use HiFolks\DataType\Arr;
// Create some fruits
$fruits = Arr::make(['๐ฅ','๐','๐','๐ฅญ','๐','๐']);
// pop (retrieve and remove) last elements
first = $fruits->first();
// Loop over fruits
$fruits->forEach(function ($element, $key) {
echo $key . " " . $element . PHP_EOL;
});
echo "First fruit: " . $first . PHP_EOL; // kiwi
Add a new element to the beginning of the array with unshift() method:
use HiFolks\DataType\Arr;
// Create some fruits
$fruits = Arr::make(['๐ฅ','๐','๐','๐ฅญ','๐','๐']);
// add a new fruit (peach) before other fruits
$fruits->unshift('๐');
// Loop over fruits
$fruits->forEach(function ($element, $key) {
echo $key . " " . $element . PHP_EOL;
});
echo PHP_EOL . "--~--" . PHP_EOL;
Method | Operation | Where | Description |
---|---|---|---|
push() | add element | to the end | Add an item to the end of an array |
pop() | remove element | from the end | Get and remove element from the end of the array |
shift() | remove element | from the begin | Get and remove the first element from the beginning of the array |
unshift() | add element | to the begin | Add a new element to the beginning of the array |
To find the index of an element you can use indexOf() method:
use HiFolks\DataType\Arr;
// Create some fruits
$fruits = Arr::make(['๐ฅ','๐','๐','๐ฅญ','๐','๐']);
echo "All fruits:" . $fruits->join(",") . PHP_EOL;
// All fruits:๐ฅ,๐,๐,๐ฅญ,๐,๐
// Find the index of an item in the Array
$pos = $fruits->indexOf('๐');
echo "Find ๐ at position: " . $pos . PHP_EOL;
// Find ๐ at position: 4
After last example ($pos === 4), you can remove an element with splice() method, using $pos and the amount of elements as arguments.
In this case, if you want to remove just one fruit (the apple) at position 4:
$removedFruits = $fruits->splice($pos, 1);
echo "Removed fruits: " . $removedFruits->join(",") . PHP_EOL;
echo "Remaining fruits:" . $fruits->join(",") . PHP_EOL;
// Removed fruits: ๐
// Remaining fruits:๐ฅ,๐,๐,๐ฅญ,๐
If you want to remove more elements (for example 10) from position 1:
// Remove items from an index position
$removedFruits = $fruits->splice(1, 10);
echo "Removed fruits: " . $removedFruits->join(",") . PHP_EOL;
echo "Remaining fruits:" . $fruits->join(",") . PHP_EOL;
// Removed fruits: ๐,๐,๐ฅญ,๐
// Remaining fruits:๐ฅ
You can use splice() method for copying (cloning) an array, with 0 as position and the length of the array as the amount of elements:
$some = $removedFruits->slice(0, $removedFruits->length());
echo "Some Fruits: " . $some->join(",") . PHP_EOL;
// Some Fruits: ๐,๐,๐ฅญ,๐
The methods listed in this article, are methods that mimic the behaviour of Javascript Array Object.
We are adding some new method as "shortcut" or "helper".
If you want to request a new method for managing array, feel free to ask, we will try to implement and add it.
24