emileperron/universal-identifier

提供 universal_identifier 函数,用于为提供的对象或数组生成唯一的 sha256 标识符。

v1.0 2021-01-22 16:05 UTC

This package is auto-updated.

Last update: 2024-09-22 23:47:31 UTC


README

为您的 PHP 基本数据类型、数组和对象生成唯一的标识符

这个小巧的 composer 包提供了一个 universal_identifier() 全局函数,可用于生成值、数组/对象的 sha256 标识符。

这些标识符可用于轻松比较代码和数据库查询中的配置,无需手动排序和比较数组或对象中的每个项目和子项目。

入门

要开始使用,请通过 Composer 需求此包。

composer require emileperron/universal-identifier

完成此操作后,您就可以在项目中开始使用 universal_identifier 函数。以下是关于库的使用和功能的简要概述。

与基本数据类型一起使用

<?php
// This will output the string's identifier: "de719c460cc38eddfb39fe286882a042be247e5d091fd8e4aed01daf9d3a5513"
echo universal_identifier("my string");

// Same thing goes for all primitives;
echo universal_identifier(150); // "7d3c18bc20c238917421291209ad0d17c83be19e4c214abcf09160af2949f591"
echo universal_identifier(0.55f); // "1c3d577cd8a09945100ac46c061835db1691907bc40f01931d5083ec7fb69def"
// etc...

与数组一起使用

<?php
$yourArray = [
	"foo" => "bar",
	"foos" => [
		"bar1", 
		"bar2"
	],
	"anotherKey" => "value",
];

// This will output the array's identifier: "7ef0675c80dd9ae9f9fed4a786f8c0641d14a646cc580de1690f62a803e54f89"
echo universal_identifier($yourArray);

// Even if you change the order of the array's keys, the identifier will remain the same (this does not apply to numerically-indexed arrays, where the order actually matters).
// Ex.:
ksort($yourArray);
// This will still output the same identifier: "7ef0675c80dd9ae9f9fed4a786f8c0641d14a646cc580de1690f62a803e54f89"
echo universal_identifier($yourArray);

贡献

如果您想添加功能或为此库提出改进建议,请随时向我提交 pull requests 到 GitHub 仓库。我会尽快查看并合并它们。

如果您遇到问题但无法立即修复,也可以提交问题。

支持

最后,如果您使用此库并希望支持我,以下是一些您可以帮助的方式: