charm/map
Composer 安装命令:
composer require charm/map
包简介
A hashmap implementation similar to Map in javascript - allowing keys and values of any data type. A small, thorough and efficient hashmap implementation.
README 文档
README
A Map is one of the most important data structures that PHP doesn't have. A high performance map is quite sophisticated and you can't really "hack it together" every time you actually need it, and simple array scanning approaches are very slow in comparison.
- Keys of any type - objects, arrays, booleans and even
null. - Values of any type.
- "Template type emulation":
Map<string, int>can be created using$map = Map::T([ 'string', 'int' ]). - Countable:
count($map) - Iterable:
foreach ($map as $key => $value) {} - Implicit item creation:
$map['newKey'][] = "Value";does not trigger a notice, even if 'newKey' does not exist.
Which problem it solves is up to you. It allows you to associate any PHP value with any other PHP value and is very useful in building graphs and caching and detecting loop recursion in tree structures.
The inherent problem with arrays:
// various types are converted to int
$array[1.23] = "Foo"; // actual value: [ 1 => "Foo" ]
$array[true] = "Foo"; // actual value: [ 1 => "Foo" ]
$array[null] = "Foo"; // actual value: [ "" => "Foo" ]
// Other types are fatal errors
$array[ [1,2,3] ]; // PHP Fatal error: Illegal offset type
$array[ $user ]; // PHP Fatal error: Illegal offset type
$array[ tmpfile() ]; // PHP Fatal error: Illegal offset type
Quick Start
$first = ["some array"];
$second = (object) ["prop" => "another array"];
$third = null;
$map = new Charm\Map();
// Any key
$map['hello'] = 'World';
$map[$first] = 'hello';
$map[$second] = $first;
$map[$third] = $map;
// Implicit value creation
$map['undefined key'][] = 1; // 'undefined key' does not exist, but appending works
$map['undefined key'][] = 2; // 'undefined key' is no longer undefined
// Iteratble
foreach ($map as $key => $value) {
}
// Countable
echo "Map has {count($map)} elements\n";
A map is different from PHP's built-in array in that you can use any value as the key. Normal arrays don't allow you to use float numbers, resource pointers or objects as a key.
Friend of a Friend traversal example (FOAF)
$friends = new Map();
$friends[$user_A][$user_B] = 1; // register all current friendships
$friends[$user_A][$user_C] = 1;
$friends[$user_B][$user_C] = 1;
$friends[$user_B][$user_D] = 1; // only $user_B is friend with $user_D
// traverse friends of friends graph
foreach ( $friends[$user_A] as $friend_1 => $distance_1) {
foreach ($friends[$friend] as $friend_2 => $distance_2) {
if ( $user_A === $friend_2 ) {
// ignore friendships back to myself
} elseif ( isset( $friends[$user_A][$friend_2] ) ) {
// i am already a friend
} else {
echo "$friend_2 is a friend of a friend to you!\n";
}
}
}
Caveats
For scalar (ints, strings, booleans and arrays) values in PHP, it is the literal value which is used as a key. If you use an array as key, and then you modify the array - the value may not be found.
$secretKey1 = ['foo'];
$secretKey2 = ['foo','bar'];
$map[$secretKey1] = "Important data";
$map[$secretKey2] = "Sensitive matters"
$secretKey1[] = 'bar'; // oops! You've modified $secretKey1 and you can't find "Important data" any more.
echo $map[$secretKey1]; // 'Sensitive matters' turns out it became identical to $secretKey2
You should avoid using arrays for keys; they are the slowest key type due to hashing.
charm/map 适用场景与选型建议
charm/map 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 charm/map 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 charm/map 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-21