PHP code example of hkp22 / php-helpers

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

    

hkp22 / php-helpers example snippets


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

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

$array = [
    'us' => 'united states',
    'uk' => 'united kingdom',
    'in' => 'india',
  ];

// run array_build function
$result = array_build($array, function ($key, $value) {
    return [strtoupper($key), ucwords($value)];
});

// Output
// ['US' => 'United States', 'UK' => 'United Kingdom', 'IN' => 'India']


[$keys, $values] = array_divide(['name' => 'Desk']);

// $keys: ['name']

// $values: ['Desk']

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

$flattened = array_dot($array);

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

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

$filtered = array_except($array, ['price']);

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

$array = [100, 200, 300];

$value = array_first($array, function ($key, $value) {
    return $value >= 150;
});

// 200

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

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

// 300

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

$flattened = array_flatten($array);

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

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

array_forget($array, 'products.desk');

// ['products' => []]

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

$price = array_get($array, 'products.desk.price');

// 100

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

array_set($array, 'products.desk.price', 200);

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

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

$contains = array_has($array, 'product.name');

// true

$contains = array_has($array, ['product.price', 'product.discount']);

// false

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

$slice = array_only($array, ['name', 'price']);

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

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

$names = array_pluck($array, 'developer.name');

// ['Taylor', 'Abigail']

$names = array_pluck($array, 'developer.name', 'developer.id');

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

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

$name = array_pull($array, 'name');

// $name: Desk

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

$value = array_pull($array, $key, $default);

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

$filtered = array_where($array, function ($value, $key) {
    return is_string($value);
});

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

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

$price = data_get($data, 'products.desk.price');

// 100

$array = [100, 200, 300];

$first = head($array);

// 100

$array = [100, 200, 300];

$last = last($array);

// 300

$converted = camel_case('foo_bar');

// fooBar

$class = class_basename('Foo\Bar\Baz');

// Baz

echo e('<html>foo</html>');

// &lt;html&gt;foo&lt;/html&gt;

$result = ends_with('This is my name', 'name');

// true

$converted = studly_case('foo_bar');

// FooBar

$traits = class_uses_recursive(App\User::class);

dd($value);

dd($value1, $value2, $value3, ...);

$traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);

$result = value(true);

// true

$result = value(function () {
    return false;
});

// false
bash
composer