![Logo](/logo.png)
# 30 seconds of php code
> A curated collection of useful PHP snippets that you can understand in 30 seconds or less.
Note: This project is inspired by [30 Seconds of Code](https://github.com/Chalarangelo/30-seconds-of-code), but there is no affiliation with that project.
## Table of Contents
### ð Array
<details>
<summary>View contents</summary>
* [`all`](#all)
* [`any`](#any)
* [`chunk`](#chunk)
* [`deepFlatten`](#deepflatten)
* [`drop`](#drop)
* [`findLast`](#findlast)
* [`findLastIndex`](#findlastindex)
* [`flatten`](#flatten)
* [`groupBy`](#groupby)
* [`hasDuplicates`](#hasduplicates)
* [`head`](#head)
* [`last`](#last)
* [`pluck`](#pluck)
* [`pull`](#pull)
* [`reject`](#reject)
* [`remove`](#remove)
* [`tail`](#tail)
* [`take`](#take)
* [`without`](#without)
* [`orderBy`](#orderby)
</details>
### â Math
<details>
<summary>View contents</summary>
* [`average`](#average)
* [`factorial`](#factorial)
* [`fibonacci`](#fibonacci)
* [`gcd`](#gcd)
* [`isEven`](#iseven)
* [`isPrime`](#isprime)
* [`lcm`](#lcm)
* [`median`](#median)
* [`maxN`](#maxn)
* [`minN`](#minn)
* [`approximatelyEqual`](#approximatelyequal)
* [`clampNumber`](#clampnumber)
</details>
### ð String
<details>
<summary>View contents</summary>
* [`endsWith`](#endswith)
* [`firstStringBetween`](#firststringbetween)
* [`isAnagram`](#isanagram)
* [`isLowerCase`](#islowercase)
* [`isUpperCase`](#isuppercase)
* [`palindrome`](#palindrome)
* [`startsWith`](#startswith)
* [`countVowels`](#countvowels)
* [`decapitalize`](#decapitalize)
</details>
### ðï¸ Function
<details>
<summary>View contents</summary>
* [`compose`](#compose)
* [`memoize`](#memoize)
* [`curry`](#curry)
* [`once`](#once)
</details>
---
## ð Array
### all
Returns `true` if the provided function returns `true` for all elements of an array, `false` otherwise.
```php
function all($items, $func)
{
return count(array_filter($items, $func)) === count($items);
}
```
<details>
<summary>Examples</summary>
```php
all([2, 3, 4, 5], function ($item) {
return $item > 1;
}); // true
```
</details>
<br>[⬠Back to top](#table-of-contents)
### any
Returns `true` if the provided function returns `true` for at least one element of an array, `false` otherwise.
```php
function any($items, $func)
{
return count(array_filter($items, $func)) > 0;
}
```
<details>
<summary>Examples</summary>
```php
any([1, 2, 3, 4], function ($item) {
return $item < 2;
}); // true
```
</details>
<br>[⬠Back to top](#table-of-contents)
### chunk
Chunks an array into smaller arrays of a specified size.
```php
function chunk($items, $size)
{
return array_chunk($items, $size);
}
```
<details>
<summary>Examples</summary>
```php
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
```
</details>
<br>[⬠Back to top](#table-of-contents)
### deepFlatten
Deep flattens an array.
```php
function deepFlatten($items)
{
$result = [];
foreach ($items as $item) {
if (!is_array($item)) {
$result[] = $item;
} else {
$result = array_merge($result, deepFlatten($item));
}
}
return $result;
}
```
<details>
<summary>Examples</summary>
```php
deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]
```
</details>
<br>[⬠Back to top](#table-of-contents)
### drop
Returns a new array with `n` elements removed from the left.
```php
function drop($items, $n = 1)
{
return array_slice($items, $n);
}
```
<details>
<summary>Examples</summary>
```php
drop([1, 2, 3]); // [2,3]
drop([1, 2, 3], 2); // [3]
```
</details>
<br>[⬠Back to top](#table-of-contents)
### findLast
Returns the last element for which the provided function returns a truthy value.
```php
function findLast($items, $func)
{
$filteredItems = array_filter($items, $func);
return array_pop($filteredItems);
}
```
<details>
<summary>Examples</summary>
```php
findLast([1, 2, 3, 4], function ($n) {
return ($n % 2) === 1;
});
// 3
```
</details>
<br>[⬠Back to top](#table-of-contents)
### findLastIndex
Returns the index of the last element for which the provided function returns a truthy value.
```php
function findLastIndex($items, $func)
{
$keys = array_keys(array_filter($items, $func));
return array_pop($keys);
}
```
<details>
<summary>Examples</summary>
```php
findLastIndex([1, 2, 3, 4], function ($n) {
return ($n % 2) === 1;
});
// 2
```
</details>
<br>[⬠Back to top](#table-of-contents)
### flatten
Flattens an array up to the one level depth.
```php
function flatten($items)
{
$result = [];
foreach ($items as $item) {
if (!is_array($item)) {
$result[] = $item;
} else {
$result = array_merge($result, array_values($item));
}
}
return $result;
}
```
<details>
<summary>Examples</summary>
```php
flatten([1, [2], 3, 4]); // [1, 2, 3, 4]
```
</details>
<br>[⬠Back to top](#table-of-contents)
### groupBy
Groups the elements of an array based on the given function.
```php
function groupBy($items, $func)
{
$group = [];
foreach ($items as $item) {
if ((!is_string($func) && is_callable($func)) || function_exists($func)) {
$key = call_user_func($func, $item);
$group[$key][] = $item;
} elseif (is_object($item)) {
$group[$item->{$func}][] = $item;
} elseif (isset($item[$func])) {
$group[$item[$func]][] = $item;
}
}
return $group;
}
```
<details>
<summary>Examples</summary>
```php
groupBy(['one', 'two', 'three'], 'strlen') // [3 => ['one', 'two'], 5 => ['three']]
```
</details>
<br>[⬠Back to top](#table-of-contents)
### hasDuplicates
Checks a flat list for duplicate values. Returns `true` if duplicate values exists and `false` if values are all unique.
```php
function hasDuplicates($items)
{
return count($items) > count(array_unique($items));
}
```
<details>
<summary>Examples</summary>
```php
hasDuplicates([1, 2, 3, 4, 5, 5]); // true
```
</details>
<br>[⬠Back to top](#table-of-contents)
### head
Returns the head of a list.
```php
function head($items)
{
return reset($items);
}
```
<details>
<summary>Examples</summary>
```php
head([1, 2, 3]); // 1
```
</details>
<br>[⬠Back to top](#table-of-contents)
### last
Returns the last element in an array.
```php
function last($items)
{
return end($items);
}
```
<details>
<summary>Examples</summary>
```php
last([1, 2, 3]); // 3
```
</details>
<br>[⬠Back to top](#table-of-contents)
### pluck
Retrieves all of the values for a given key:
```php
function pluck($items, $key)
{
return array_map( function($item) use ($key) {
return is_object($item) ? $item->$key : $item[$key];
}, $items);
}
```
<details>
<summary>Examples</summary>
```php
pluck([
['product_id' => 'prod-100', 'name' => 'Desk'],
['product_id' => 'prod-200', 'name' => 'Chair'],
], 'name');
// ['Desk', 'Chair']
```
</details>
<br>[⬠Back to top](#table-of-contents)
### pull
Mutates the original array to filter out the values specified.
```php
function pull(&$items, ...$params)
{
$items = array_values(array_diff($items, $params));
return $items;
}
```
<details>
<summary>Examples</summary>
```php
$items = ['a', 'b', 'c', 'a', 'b', 'c'];
pull($items, 'a', 'c'); // $items will be ['b', 'b']
```
</details>
<br>[⬠Back to top](#table-of-contents)
### reject
Filters the collection using the given callback.
```php
function reject($items, $func)
{
return array_values(array_diff($items, array_filter($items, $func)));
}
```
<details>
<summary>Examples</summary>
```php
reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) {
return strlen($item) > 4;
}); // ['Pear', 'Kiwi']
```
</details>
<br>[⬠Back to top](#table-of-contents)
### remove
Removes elements from an array for which the given function returns false.
```php
function remove($items, $func)
{
$filtered = arra
没有合适的资源?快使用搜索试试~ 我知道了~
精选的 PHP 代码片段和学习资料可以让你在 30 秒内学会
共64个文件
md:48个
php:6个
json:3个
需积分: 5 0 下载量 171 浏览量
2024-03-01
11:44:10
上传
评论 1
收藏 62KB ZIP 举报
温馨提示
精选的 PHP 代码片段可以让您在30秒内快速学习和了解 PHP 编程。这些精彩的片段涵盖了各种主题,从字符串操作到数组处理,再到文件操作和数据库连接,无所不包。只需阅读简短的代码示例,您就能快速掌握 PHP 的基础知识和高级技巧。例如,您可以学习如何使用内置函数处理字符串,比如字符串截取、替换、连接和格式化。您还可以了解如何创建和操作数组,包括排序、过滤和遍历数组中的元素。此外,您还可以学习如何读取和写入文件,以及如何执行数据库查询和更新操作。通过学习这些代码片段,您可以了解 PHP 的基本语法和常用函数,并学会如何处理字符串、操作数组、读写文件以及连接数据库。这些知识可以帮助您更加高效地编写 PHP 程序,并解决实际的编程问题。无论您是初学者还是有经验的开发人员,这些代码片段都能帮助您提高编程技能,让您在 PHP 领域更加游刃有余。PHP 是一种广泛使用的服务器端脚本语言,它具有简单易学、灵活强大的特点,适合用于开发各种类型的网站和应用程序。这里是一些精选的 PHP 代码片段,可以帮助您快速学习和了解 PHP 编程。学习这些高级技巧,您还可以进一步提升 PHP 编程的能力和效率。
资源推荐
资源详情
资源评论
收起资源包目录
精选的 PHP 代码片段可以让你在 30 秒内学会.zip (64个子文件)
30-seconds-of-php-code-master
snippets
isLowerCase.md 317B
reject.md 361B
deepFlatten.md 434B
chunk.md 267B
flatten.md 434B
curry.md 866B
findLast.md 370B
palindrome.md 304B
pull.md 355B
without.md 295B
hasDuplicates.md 348B
memoize.md 788B
tail.md 260B
factorial.md 254B
head.md 186B
lcm.md 377B
gcd.md 402B
firstStringBetween.md 392B
isUpperCase.md 319B
average.md 258B
minN.md 421B
decapitalize.md 502B
startsWith.md 276B
isEven.md 229B
once.md 477B
maxN.md 418B
isPrime.md 369B
clampNumber.md 416B
countVowels.md 374B
any.md 349B
findLastIndex.md 392B
compose.md 654B
pluck.md 437B
drop.md 268B
endsWith.md 305B
take.md 287B
approximatelyEqual.md 509B
all.md 355B
remove.md 376B
isAnagram.md 319B
orderBy.md 748B
last.md 194B
median.md 424B
fibonacci.md 377B
groupBy.md 690B
.travis.yml 1KB
phpunit.xml.dist 410B
src
helpers.php 7KB
data
tags.json 252B
database.json 930B
LICENSE 1KB
logo.png 28KB
composer.json 647B
tests
FunctionTest.php 1KB
StringTest.php 2KB
MathTest.php 2KB
ArrayTest.php 5KB
CONTRIBUTING.md 538B
.travis
push.sh 994B
.gitignore 31B
README.md 21KB
scripts
snippet-template.md 265B
generate 2KB
build.php 2KB
共 64 条
- 1
资源评论
传奇开心果编程
- 粉丝: 7930
- 资源: 336
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功