hkp22/php-helpers

Php Helpers,受 Laravel Helper 启发,用于非 Laravel 项目。

v1.01 2018-06-13 19:25 UTC

This package is auto-updated.

Last update: 2024-09-21 20:20:48 UTC


README

此项目从 Laravel 框架中提取了有用的辅助函数,可以在 Laravel 之外使用。

安装

您可以通过 composer 安装此包

composer require hkp22/php-helpers

用法

数组

array_add()

array_add 函数在给定的键/值对不存在于数组中时,将其添加到数组中

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

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

array_build()

使用回调函数构建新数组。

$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']

array_divide()

array_divide 函数返回两个数组,一个包含键,另一个包含给定数组的值

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

// $keys: ['name']

// $values: ['Desk']

array_dot()

array_dot 函数将多维数组展平到一个单层数组,该数组使用“点”符号来指示深度

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

$flattened = array_dot($array);

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

array_except()

array_except 函数从数组中删除给定的键/值对

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

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

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

array_first()

array_first 函数返回通过给定真值测试的数组的第一个元素

$array = [100, 200, 300];

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

// 200

array_last()

array_last 函数返回通过给定真值测试的数组的最后一个元素

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

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

// 300

array_flatten()

array_flatten 函数将多维数组展平到一个单层数组

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

$flattened = array_flatten($array);

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

array_forget()

array_forget 函数使用“点”符号从深度嵌套的数组中删除给定的键/值对

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

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

// ['products' => []]

array_get()

array_get 函数使用“点”符号从深度嵌套的数组中检索值

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

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

// 100

array_set()

array_set 函数使用“点”符号在深度嵌套的数组中设置值

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

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

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

array_has()

array_has 函数检查使用“点”符号是否在数组中存在给定的项或项

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

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

// true

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

// false

array_only()

array_only 函数从给定的数组中只返回指定的键/值对

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

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

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

array_pluck()

array_pluck 函数从数组中检索给定键的所有值

$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_pull()

array_pull 函数从数组中返回并删除键/值对

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

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

// $name: Desk

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

可以将默认值作为方法的第三个参数传递。如果键不存在,将返回此值

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

array_where()

array_where 函数使用给定的闭包过滤数组

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

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

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

data_get()

data_get 函数使用“点”符号从嵌套数组或对象中检索值

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

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

// 100

head()

head 函数返回给定数组中的第一个元素

$array = [100, 200, 300];

$first = head($array);

// 100

last()

last 函数返回给定数组中的最后一个元素

$array = [100, 200, 300];

$last = last($array);

// 300

字符串

camel_case()

camel_case 函数将给定的字符串转换为 camelCase

$converted = camel_case('foo_bar');

// fooBar

class_basename()

class_basename 返回给定类的类名,其中已移除类的命名空间

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

// Baz

e()

e 函数默认情况下使用 PHP 的 htmlspecialchars 函数,将 double_encode 选项设置为 true

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

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

ends_with()

ends_with 函数确定给定的字符串是否以给定的值结束

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

// true

studly_case()

studly_case 函数将给定的字符串转换为 StudlyCase

$converted = studly_case('foo_bar');

// FooBar

杂项

class_uses_recursive()

class_uses_recursive 函数返回一个类所使用的所有特性,包括其所有父类所使用的特性。

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

dd()

dd 函数输出给定的变量并结束脚本的执行。

dd($value);

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

trait_uses_recursive()

trait_uses_recursive 函数返回一个特性所使用的所有特性。

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

value()

value 函数返回它所接收的值。然而,如果您向该函数传递一个闭包(Closure),则闭包将被执行,然后返回其结果。

$result = value(true);

// true

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

// false