arraypress/wp-array-utils
Composer 安装命令:
composer require arraypress/wp-array-utils
包简介
Essential array operations for WordPress development - lean, focused, and practical
关键字:
README 文档
README
A lean PHP library focused on the most commonly needed array operations in WordPress development. Simple, predictable methods that you'll reach for daily.
Features
- 🎯 Daily Essentials: Only the array operations you actually use regularly
- 📍 Dot Notation: Access nested array values with simple dot syntax
- 🔍 Smart Filtering: Keep or exclude keys with clean syntax
- 📊 Data Processing: Group, pluck, and sort arrays efficiently
- 🔗 Array Comparison: Check matches between arrays easily
- 🎛️ WordPress Ready: Convert arrays for select fields and forms
- ⚡ Lean & Fast: Focused on practical operations without bloat
- 🔒 Safe Operations: Graceful handling of missing keys and invalid data
Requirements
- PHP 7.4 or later
Installation
composer require arraypress/wp-array-utils
Basic Usage
Array Manipulation
use ArrayPress\ArrayUtils\Arr; // Get specific values $first = Arr::first(['a', 'b', 'c']); // 'a' $last = Arr::last(['a', 'b', 'c']); // 'c' // Filter arrays by keys $allowed = Arr::only($user_data, ['name', 'email']); // Keep only these keys $clean = Arr::except($user_data, ['password', 'secret']); // Remove these keys // Sort arrays (with absint filtering for numeric) $sorted = Arr::sort_numeric([3, 1, 4, 1, 5]); // [1, 1, 3, 4, 5] $alphabetical = Arr::sort_alphabetic(['zebra', 'apple', 'banana']); $by_column = Arr::sort_by_column($users, 'name'); // Sort by name column $by_key = Arr::sort_by_key(['zebra' => 1, 'apple' => 2]); // Sort by array keys
Dot Notation Access
// Access nested data easily $config = [ 'database' => [ 'connections' => [ 'mysql' => ['host' => 'localhost'] ] ] ]; $host = Arr::get($config, 'database.connections.mysql.host'); // 'localhost' $port = Arr::get($config, 'database.connections.mysql.port', 3306); // 3306 (default) // Set nested values $config = Arr::set($config, 'database.connections.mysql.port', 3307); // Check if nested key exists if (Arr::has($config, 'database.connections.redis')) { // Redis connection exists }
Grouping and Aggregation
$users = [ ['name' => 'John', 'role' => 'admin', 'status' => 'active'], ['name' => 'Jane', 'role' => 'user', 'status' => 'active'], ['name' => 'Bob', 'role' => 'admin', 'status' => 'inactive'] ]; // Group by role $by_role = Arr::group_by($users, 'role'); // ['admin' => [...], 'user' => [...]] // Pluck specific values $names = Arr::pluck($users, 'name'); // ['John', 'Jane', 'Bob'] // Flatten nested arrays $nested = [ 'fruits' => ['apple', 'banana'], 'colors' => ['red', 'blue'] ]; $flat = Arr::flatten($nested); // ['apple', 'banana', 'red', 'blue']
Array Manipulation
$menu = ['home' => 'Home', 'contact' => 'Contact']; // Insert after specific key $menu = Arr::insert_after($menu, 'home', ['about' => 'About']); // ['home' => 'Home', 'about' => 'About', 'contact' => 'Contact'] // Insert before specific key $menu = Arr::insert_before($menu, 'contact', ['services' => 'Services']); // Shuffle array $shuffled = Arr::shuffle([1, 2, 3, 4, 5]);
Array Comparison (Common Patterns)
// Check if all elements in one array exist in another $permissions = ['read', 'write', 'delete']; $user_perms = ['read', 'write']; $has_all = Arr::has_all_matches($user_perms, $permissions); // true // Check if any elements match between arrays $arr1 = ['apple', 'banana']; $arr2 = ['banana', 'cherry']; $has_any = Arr::has_any_matches($arr1, $arr2); // true (banana matches) // Common WordPress use case - checking post capabilities $required_caps = ['edit_posts', 'delete_posts']; $user_caps = ['edit_posts', 'delete_posts', 'manage_options']; $can_do_all = Arr::has_all_matches($required_caps, $user_caps); // true
WordPress Select Field Integration
// Convert regular array to select field options format $post_types = ['post' => 'Posts', 'page' => 'Pages', 'product' => 'Products']; $options = Arr::to_options($post_types); // [ // ['value' => 'post', 'label' => 'Posts'], // ['value' => 'page', 'label' => 'Pages'], // ['value' => 'product', 'label' => 'Products'] // ] // Convert back from options format $original = Arr::from_options($options); // ['post' => 'Posts', 'page' => 'Pages', 'product' => 'Products']
Format Conversion
// Convert to string format $tags = ['wordpress', 'php', 'javascript']; $string = Arr::to_string($tags); // 'wordpress,php,javascript' $string = Arr::to_string($tags, ' | '); // 'wordpress | php | javascript' $quoted = Arr::to_string($tags, ',', '"'); // '"wordpress","php","javascript"'
Use Cases
WordPress Post Processing
function process_posts($posts) { // Group posts by status $by_status = Arr::group_by($posts, 'post_status'); // Get all post IDs for bulk operations $post_ids = Arr::pluck($posts, 'ID'); // Sort posts by title $sorted = Arr::sort_by_column($posts, 'post_title'); return [ 'by_status' => $by_status, 'post_ids' => $post_ids, 'sorted' => $sorted ]; }
Settings Management
function get_nested_setting($settings, $path, $default = null) { return Arr::get($settings, $path, $default); } // Usage $host = get_nested_setting($config, 'database.mysql.host', 'localhost'); $debug = get_nested_setting($config, 'app.debug', false);
Permission Checking
function user_can_perform_actions($user_capabilities, $required_actions) { return Arr::has_all_matches($required_actions, $user_capabilities); } // Usage $required = ['edit_posts', 'delete_posts']; $user_caps = ['edit_posts', 'delete_posts', 'manage_options']; $can_edit = user_can_perform_actions($user_caps, $required); // true
Menu Management
function add_menu_item_after($menu, $after_key, $new_item) { return Arr::insert_after($menu, $after_key, $new_item); } // Usage - add "About" after "Home" in navigation $menu = ['home' => 'Home', 'services' => 'Services', 'contact' => 'Contact']; $menu = add_menu_item_after($menu, 'home', ['about' => 'About Us']);
API Reference
Arr Class
Selection:
first( array $array )- Get first elementlast( array $array )- Get last element
Filtering:
only( array $array, array $keys)- Keep only specified keysexcept( array $array, array $keys)- Remove specified keys
Dot Notation:
get( array $array, string $path, $default = null)- Get nested valueset( array $array, string $path, $value)- Set nested valuehas( array $array, string $path)- Check if nested key exists
Sorting:
sort_numeric( array $array, bool $desc = false)- Sort numeric values (with absint)sort_alphabetic( array $array, bool $desc = false)- Sort alphabeticallysort_by_key( array $array, bool $desc = false)- Sort by array keyssort_by_column( array $array, string $key, bool $desc = false)- Sort by column
Data Processing:
group_by( array $array, string $key)- Group by key valuepluck( array $array, string $key)- Extract column valuesflatten( array $array, bool $unique = false)- Flatten multidimensional arrays
Array Manipulation:
insert_after( array $array, string $key, array $new)- Insert after keyinsert_before( array $array, string $key, array $new)- Insert before keyshuffle( array $array)- Shuffle array elements
Array Comparison:
has_all_matches( array $array1, array $array2)- Check if all elements in array1 exist in array2has_any_matches( array $array1, array $array2)- Check if any elements match
Conversion:
to_string( array $array, string $delimiter = ',')- Convert to delimited string
WordPress Helpers:
to_options( array $array)- Convert to select field formatfrom_options(array $options)- Convert from select field format
Error Handling
All methods return sensible defaults for invalid inputs rather than throwing exceptions:
- Missing array keys return provided defaults
- Empty arrays return null or empty arrays as appropriate
- Invalid operations return the original array unchanged
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GPL-2.0-or-later License.
Support
arraypress/wp-array-utils 适用场景与选型建议
arraypress/wp-array-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「array」 「utilities」 「manipulation」 「sorting」 「dot-notation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arraypress/wp-array-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arraypress/wp-array-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arraypress/wp-array-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
A collection of enhancements and utilities for the Silverstripe UserForms module
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
Convert array or camel case to underscore, or underscore to others.
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2025-09-02