PHP code example of hi-folks / array

1. Go to this page and download the library: Download hi-folks/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/ */

    

hi-folks / array example snippets


$fruits = Arr::make([
    'green' => [
        'kiwi' => '🥝',
        'mango' => '🥭'
    ],
    'red' => [
        'strawberry' => '🍓',
        'apple' => '🍎'
    ],
    'yellow' => [
        'lemon' => '🍋',
        'banana' => '🍌',
    ]
]);
$fruits->get('red'); // 🍓,🍎
$fruits->get('red.strawberry'); // 🍓

$fruits->get('red#strawberry', charNestedKey: '#'); // 🍓

$fruits->get('red#somestrangefruit',
'🫠', '#'); // 🫠

    $arr = Arr::make(
        [
            "avocado" =>
                [
                    'name' => 'Avocado',
                    'fruit' => '🥑',
                    'wikipedia' => 'https://en.wikipedia.org/wiki/Avocado'
                ],
            "apple" =>
                [
                    'name' => 'Apple',
                    'fruit' => '🍎',
                    'wikipedia' => 'https://en.wikipedia.org/wiki/Apple'
                ],
            "banana" =>
                [
                    'name' => 'Banana',
                    'fruit' => '🍌',
                    'wikipedia' => 'https://en.wikipedia.org/wiki/Banana'
                ],
            "cherry" =>
                [
                    'name' => 'Cherry',
                    'fruit' => '🍒',
                    'wikipedia' => 'https://en.wikipedia.org/wiki/Cherry'
                ],
        ]
    );
$appleArr = $arr->getArr("apple")
// $appleArr is an Arr instance so that you can access
// to the Arr methods like count()
$arr->getArr("apple")->count();


$articleText = "Some words as a sample sentence";
$textField = Arr::make();
$textField->set("type", "doc");
$textField->set("content.0.content.0.text", $articleText);
$textField->set("content.0.content.0.type", "text");
$textField->set("content.0.type", "paragraph");

// Load the vendor/autoload file
olks\DataType\Arr;
// use static make method to create Arr object
$arr = Arr::make();
$arr->push('Hi');
$arr->push('Folks');
echo $arr->length();
// to access to the "native" PHP array:
print_r($arr->arr());


use HiFolks\DataType\Arr;
$arr = Arr::fromFunction(fn () => random_int(0, 100), 500);


use HiFolks\DataType\Arr;
$arr = Arr::make();
$arr[] = "First element";
$arr[] = "Second element";
$count = $arr->length();
// output: 2
$arr->reverse();
echo $arr[0];
// output: Second element

use HiFolks\DataType\Table;
$dataTable = [
    ['product' => 'Desk', 'price' => 200, 'active' => true],
    ['product' => 'Chair', 'price' => 100, 'active' => true],
    ['product' => 'Door', 'price' => 300, 'active' => false],
    ['product' => 'Bookcase', 'price' => 150, 'active' => true],
    ['product' => 'Door', 'price' => 100, 'active' => true],
];
$table = Table::make($dataTable);
$arr = $table
    ->select('product' , 'price')
    ->where('price', ">", 100)
    ->calc('new_field', fn ($item) => $item['price'] * 2);

var_dump($textField->arr());

array(2) {
  ["type"]=>
  string(3) "doc"
  ["content"]=>
  array(1) {
    [0]=>
    array(2) {
      ["content"]=>
      array(1) {
        [0]=>
        array(2) {
          ["text"]=>
          string(31) "Some words as a sample sentence"
          ["type"]=>
          string(4) "text"
        }
      }
      ["type"]=>
      string(9) "paragraph"
    }
  }
}

[
    ['product' => 'Desk', 'price' => 200, 'active' => true],
    ['product' => 'Chair', 'price' => 100, 'active' => true],
    ['product' => 'Door', 'price' => 300, 'active' => false],
    ['product' => 'Bookcase', 'price' => 150, 'active' => true],
    ['product' => 'Door', 'price' => 100, 'active' => true],
]

[
    ['product' => 'Desk', 'price' => 200, 'active' => true],
    ['product' => 'Chair', 'price' => 100, 'active' => true],
    ['product' => 'Door', 'price' => 300, 'active' => false],
    ['product' => 'Bookcase', 'price' => 150, 'active' => true],
    ['product' => 'Door', 'price' => 100, 'active' => true],
]