38
loading...
This website collects cookies to deliver better user experience
composer require hi-folks/array
require("./vendor/autoload.php");
use HiFolks\DataType\Arr;
use HiFolks\DataType\Arr;
$fruits = Arr::make(['🍎', '🍌']);
echo $fruits->length();
// Output: 2
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;
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 🍌;
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;
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
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
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 |
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
$removedFruits = $fruits->splice($pos, 1);
echo "Removed fruits: " . $removedFruits->join(",") . PHP_EOL;
echo "Remaining fruits:" . $fruits->join(",") . PHP_EOL;
// Removed fruits: 🍎
// Remaining fruits:🥝,🍓,🍋,🥭,🍌
// 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:🥝
$some = $removedFruits->slice(0, $removedFruits->length());
echo "Some Fruits: " . $some->join(",") . PHP_EOL;
// Some Fruits: 🍓,🍋,🥭,🍌