Hiện tại, nó có thể thay đổi trường hợp chuỗi khóa thành trường hợp lạc đà, chữ thường hoặc sử dụng thay đổi tùy chỉnh bằng chức năng đóng
Tháng 5 năm 2018
Number 7PHP cung cấp nhiều hàm thao tác mảng cho nhiều mục đích khác nhau, nhưng không phải cho tất cả các mục đích.
Mảng kết hợp có thể kết hợp các chuỗi văn bản với một giá trị được kết hợp nhưng văn bản khóa phân biệt chữ hoa chữ thường
Gói này cung cấp một giải pháp có thể tạo lại một mảng kết hợp bằng cách thay đổi trường hợp chuỗi của mỗi khóa
Manuel Lemos
Người được đề cử. 5x
Thư viện đơn giản để xử lý chuyển đổi chữ hoa chữ thường từ các khóa mảng
Cài đặt
composer require deoliveiralucas/array-keys-case-transformCách sử dụng
use ArrayKeysCaseTransform\ArrayKeys; $input = [ 'first_key' => 'value' ]; print_r(ArrayKeys::toCamelCase($input)); /* Output: Array ( [firstKey] => value ) */ $input = [ 'firstKey' => 'value' ]; print_r(ArrayKeys::toSnakeCase($input)); /* Output: Array ( [first_key] => value ) */định dạng tùy chỉnh
use ArrayKeysCaseTransform\ArrayKeys; use ArrayKeysCaseTransform\Transformer\AbstractTransformer; $input = [ 'firstKey' => 'value' ]; $customTransform = new class extends AbstractTransformer { protected function format(string $key) : string { return str_replace('Key', 'CustomKey', $key); } }; print_r(ArrayKeys::transform($customTransform, $input)); /* Output: Array ( [firstCustomKey] => value ) */Đóng góp
Vui lòng xem ĐÓNG GÓP để biết chi tiết
Giấy phép
ArrayKeysCaseTransform được phát hành theo Giấy phép MIT. Vui lòng xem Tệp giấy phép để biết thêm thông tin
Bạn có thể sử dụng lớp bên dưới để chuyển đổi tất cả các khóa thành trường hợp con rắn trong mảng đã cho
class ArrayKeyCaseConverter
{
public function snakeCase(array $array): array
{
return array_map(
function($item) {
if (is_array($item)) {
$item = $this->snakeCase($item);
}
return $item;
},
$this->doSnakeCase($array)
);
}
private function doSnakeCase(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
$key = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $key));
$result[$key] = $value;
}
return $result;
}
}
$in = [
'jolly' => 'jolly',
'Jolly' => 'Jolly',
'JOLLY' => 'JOLLY',
'jollyGood' => 'jollyGood',
'JollyGood' => 'JollyGood',
'Jolly1' => [
'Jolly',
],
'Jolly2Jolly' => [
'jolly' => 'jolly',
'Jolly' => 'Jolly',
'JOLLY' => 'JOLLY',
'jollyGood' => 'jollyGood',
'JollyGood' => 'JollyGood',
'JollyJollyJolly' => [
'HiHi' => 'HiHi',
'be_be' => 'be_be',
],
],
];
Array of partitions with always the same number of sub-array indexes.
function partitionFixedSubArray($array, $length, $empty_space)
{
$result = [];
________số 8
$result = array_chunk($array, $length);
$last_array_count = sizeof(end($result));
use ArrayKeysCaseTransform\ArrayKeys; $input = [ 'first_key' => 'value' ]; print_r(ArrayKeys::toCamelCase($input)); /* Output: Array ( [firstKey] => value ) */ $input = [ 'firstKey' => 'value' ]; print_r(ArrayKeys::toSnakeCase($input)); /* Output: Array ( [first_key] => value ) */ 0
use ArrayKeysCaseTransform\ArrayKeys; $input = [ 'first_key' => 'value' ]; print_r(ArrayKeys::toCamelCase($input)); /* Output: Array ( [firstKey] => value ) */ $input = [ 'firstKey' => 'value' ]; print_r(ArrayKeys::toSnakeCase($input)); /* Output: Array ( [first_key] => value ) */ 1
use ArrayKeysCaseTransform\ArrayKeys; $input = [ 'first_key' => 'value' ]; print_r(ArrayKeys::toCamelCase($input)); /* Output: Array ( [firstKey] => value ) */ $input = [ 'firstKey' => 'value' ]; print_r(ArrayKeys::toSnakeCase($input)); /* Output: Array ( [first_key] => value ) */ 2
use ArrayKeysCaseTransform\ArrayKeys; $input = [ 'first_key' => 'value' ]; print_r(ArrayKeys::toCamelCase($input)); /* Output: Array ( [firstKey] => value ) */ $input = [ 'firstKey' => 'value' ]; print_r(ArrayKeys::toSnakeCase($input)); /* Output: Array ( [first_key] => value ) */ 3
use ArrayKeysCaseTransform\ArrayKeys; $input = [ 'first_key' => 'value' ]; print_r(ArrayKeys::toCamelCase($input)); /* Output: Array ( [firstKey] => value ) */ $input = [ 'firstKey' => 'value' ]; print_r(ArrayKeys::toSnakeCase($input)); /* Output: Array ( [first_key] => value ) */ 4
";print_r(partitionFixedSubArray($employees, 5, "empty space"));
echo "Array of partitions with always the same number of sub-array indexes.0
Array of partitions with always the same number of sub-array indexes.1