30秒的PHP代码片段-ARRAY

本文来自GitHub开源项目

点我跳转

30秒的PHP代码片段

 

精选的有用PHP片段集合,您可以在30秒或更短的时间内理解这些片段。

排列

all

如果所提供的函数返回 true 的数量等于数组中成员数量的总和,则函数返回 true,否则返回 false

function all($items, $func) { return count(array_filter($items, $func)) === count($items); }

Examples

all([2, 3, 4, 5], function ($item) { return $item > 1; }); // true

any

如果提供的函数对数组中的至少一个元素返回true,则返回true,否则返回false

function any($items, $func) { return count(array_filter($items, $func)) > 0; }

Examples

any([1, 2, 3, 4], function ($item) { return $item < 2; }); // true

deepFlatten(深度平铺数组)

将多维数组转为一维数组

function deepFlatten($items) { $result = []; foreach ($items as $item) { if (!is_array($item)) { $result[] = $item; } else { $result = array_merge($result, deepFlatten($item)); } } return $result; }

Examples

deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]

drop

返回一个新数组,并从左侧弹出n个元素。

function drop($items, $n = 1) { return array_slice($items, $n); }

Examples

drop([1, 2, 3]); // [2,3] drop([1, 2, 3], 2); // [3]

findLast

返回所提供的函数为其返回的有效值(即过滤后的值)的最后一个元素的键值(value)。

function findLast($items, $func) { $filteredItems = array_filter($items, $func); return array_pop($filteredItems); }

Examples

findLast([1, 2, 3, 4], function ($n) { return ($n % 2) === 1; }); // 3

findLastIndex

返回所提供的函数为其返回的有效值(即过滤后的值)的最后一个元素的键名(key)。

function findLastIndex($items, $func) { $keys = array_keys(array_filter($items, $func)); return array_pop($keys); }

Examples

findLastIndex([1, 2, 3, 4], function ($n) { return ($n % 2) === 1; }); // 2

flatten(平铺数组)

将数组降为一维数组

function flatten($items) { $result = []; foreach ($items as $item) { if (!is_array($item)) { $result[] = $item; } else { $result = array_merge($result, array_values($item)); } } return $result; }

Examples

flatten([1, [2], 3, 4]); // [1, 2, 3, 4]

groupBy

根据给定的函数对数组的元素进行分组。

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; }

Examples

groupBy(['one', 'two', 'three'], 'strlen'); // [3 => ['one', 'two'], 5 => ['three']]

hasDuplicates(查重)

检查数组中的重复值。如果存在重复值,则返回true;如果所有值都是唯一的,则返回false

function hasDuplicates($items) { return count($items) > count(array_unique($items)); }

Examples

hasDuplicates([1, 2, 3, 4, 5, 5]); // true

head

返回数组中的第一个元素。

function head($items) { return reset($items); }

Examples

head([1, 2, 3]); // 1

last

返回数组中的最后一个元素。

function last($items) { return end($items); }

Examples

last([1, 2, 3]); // 3

pluck

检索给定键名的所有键值

function pluck($items, $key) { return array_map( function($item) use ($key) { return is_object($item) ? $item->$key : $item[$key]; }, $items); }

Examples

pluck([
    ['product_id' => 'prod-100', 'name' => 'Desk'], ['product_id' => 'prod-200', 'name' => 'Chair'], ], 'name'); // ['Desk', 'Chair']

pull

修改原始数组以过滤掉指定的值。

function pull(&$items, ...$params) { $items = array_values(array_diff($items, $params)); return $items; }

Examples

$items = ['a', 'b', 'c', 'a', 'b', 'c']; pull($items, 'a', 'c'); // $items will be ['b', 'b']

reject

使用给定的回调筛选数组。

function reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); }

Examples

reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; }); // ['Pear', 'Kiwi']

remove

从给定函数返回false的数组中删除元素。

function remove($items, $func) { $filtered = array_filter($items, $func); return array_diff_key($items, $filtered); }

Examples

remove([1, 2, 3, 4], function ($n) { return ($n % 2) === 0; }); // [0 => 1, 2 => 3]

tail

返回数组中的所有元素,第一个元素除外。

function tail($items) { return count($items) > 1 ? array_slice($items, 1) : $items; }

Examples

tail([1, 2, 3]); // [2, 3]

take

返回一个数组,其中从开头删除了n个元素。

function take($items, $n = 1) { return array_slice($items, 0, $n); }

Examples

take([1, 2, 3], 5); // [1, 2, 3] take([1, 2, 3, 4, 5], 2); // [1, 2]

without

筛选出给定值之外的数组元素。

function without($items, ...$params) { return array_values(array_diff($items, $params)); }

Examples

without([2, 1, 2, 3, 5, 8], 1, 2, 8); // [3, 5]

orderBy

按键名对数组或对象的集合进行排序。

function orderBy($items, $attr, $order) { $sortedItems = []; foreach ($items as $item) { $key = is_object($item) ? $item->{$attr} : $item[$attr]; $sortedItems[$key] = $item; } if ($order === 'desc') { krsort($sortedItems); } else { ksort($sortedItems); } return array_values($sortedItems); }

Examples

orderBy(
    [
        ['id' => 2, 'name' => 'Joy'], ['id' => 3, 'name' => 'Khaja'], ['id' => 1, 'name' => 'Raja'] ], 'id', 'desc' ); // [['id' => 3, 'name' => 'Khaja'], ['id' => 2, 'name' => 'Joy'], ['id' => 1, 'name' => 'Raja']]

 

转载于:https://www.cnblogs.com/gongchixin/articles/10395508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
moodle-mod_hvp是Moodle平台上的一个模块,用于创建交互式视频内容。下面是db/upgrade.php文件的详细解释: 1. 文件头部分:定义了文件的版权信息和依赖关系。 ``` defined('MOODLE_INTERNAL') || die(); $plugin->version = 2020071000; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2018051700; // Requires this Moodle version. $plugin->component = 'mod_hvp'; // Full name of the plugin (used for diagnostics). ``` 2. upgrade_plugin_savepoint()函数:该函数用于升级插件版本,如果需要在升级过程中执行SQL语句,则需要在该函数中添加。 ``` function xmldb_hvp_upgrade($oldversion) { global $DB; $dbman = $DB->get_manager(); if ($oldversion < 2019072200) { // Define table hvp_quiz_attempt to be created. $table = new xmldb_table('hvp_quiz_attempt'); // Adding fields to table hvp_quiz_attempt. $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('hvpquiz', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, null, null, null); $table->add_field('started', XMLDB_TYPE_INTEGER, '10', null, null, null, null); $table->add_field('finished', XMLDB_TYPE_INTEGER, '10', null, null, null, null); $table->add_field('score', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null); $table->add_field('status', XMLDB_TYPE_INTEGER, '2', null, null, null, null); // Adding keys to table hvp_quiz_attempt. $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); // Conditionally launch create table for hvp_quiz_attempt. if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // HVP savepoint reached. upgrade_plugin_savepoint(true, 2019072200, 'mod', 'hvp'); } } ``` 3. 添加表格和字段:在该升级脚本中,我们创建了一个名为hvp_quiz_attempt的表格,并在该表格中添加了一些字段。 ``` // Define table hvp_quiz_attempt to be created. $table = new xmldb_table('hvp_quiz_attempt'); // Adding fields to table hvp_quiz_attempt. $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('hvpquiz', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, null, null, null); $table->add_field('started', XMLDB_TYPE_INTEGER, '10', null, null, null, null); $table->add_field('finished', XMLDB_TYPE_INTEGER, '10', null, null, null, null); $table->add_field('score', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null); $table->add_field('status', XMLDB_TYPE_INTEGER, '2', null, null, null, null); // Adding keys to table hvp_quiz_attempt. $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); // Conditionally launch create table for hvp_quiz_attempt. if (!$dbman->table_exists($table)) { $dbman->create_table($table); } ``` 4. 升级插件版本:在升级过程中,我们需要确保新的升级脚本只运行一次。这里使用upgrade_plugin_savepoint()函数来实现,该函数在第一次运行时创建一个保存点,然后将保存点的版本与插件版本进行比较。如果插件版本低于保存点版本,则运行升级脚本。 ``` // HVP savepoint reached. upgrade_plugin_savepoint(true, 2019072200, 'mod', 'hvp'); ``` 上述代码片段中的`2019072200`是一个版本号,它代表了插件的当前版本。当插件版本发生更改时,该版本号应更新为新的版本号。 总的来说,db/upgrade.php文件是用于升级Moodle模块的重要文件,它可以确保插件的数据库结构得到正确的更新和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值