PHP 中内置对集合的支持吗?

发布于 2024-12-16 11:01:47 字数 115 浏览 0 评论 0原文

我正在寻找一种在 php 中创建数组的简单方法,该方法不允许重复条目,但允许轻松组合其他集或数组。

我最感兴趣的是该语言中是否存在这样的功能,因为编写我自己的功能并不困难。我只是不想,如果不需要的话。

I'm looking for a simple way to create an array in php that will not allow duplicate entries, but allows for easy combining of other sets or arrays.

I'm mostly interested in whether such a feature exists in the language because writing my own wouldn't be difficult. I just don't want to if I don't need to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

垂暮老矣 2024-12-23 11:01:47

只是一个想法,如果您使用数组键而不是值,您将确保没有重复项,而且这还可以轻松合并两个“集”。

$set1 = array ('a' => 1, 'b' => 1, );
$set2 = array ('b' => 1, 'c' => 1, );
$union = $set1 + $set2;

Just an idea, if you use the array keys instead of values, you'll be sure there are no duplicates, also this allows for easy merging of two "sets".

$set1 = array ('a' => 1, 'b' => 1, );
$set2 = array ('b' => 1, 'c' => 1, );
$union = $set1 + $set2;
忘年祭陌 2024-12-23 11:01:47

答案是否定的,PHP内部没有原生的set解决方案。有一个 Set 数据结构 ,但这不是基线 PHP。

在任何语言中使用映射(即关联数组)实现集合都有一个约定。对于 PHP,您应该使用 true 作为底部值。

<?php

$left = [1=>true, 5=>true, 7=>true];
$right = [6=>true, 7=>true, 8=>true, 9=>true];

$union = $left + $right;
$intersection = array_intersect_assoc($left, $right);

var_dump($left, $right, $union, $intersection);

The answer is no, there is not a native set solution inside PHP. There is a Set data structure, but that is not baseline PHP.

There is a convention for implementing sets using maps (i.e. associative arrays) in any language. And for PHP you should use true as the bottom value.

<?php

$left = [1=>true, 5=>true, 7=>true];
$right = [6=>true, 7=>true, 8=>true, 9=>true];

$union = $left + $right;
$intersection = array_intersect_assoc($left, $right);

var_dump($left, $right, $union, $intersection);
不知在何时 2024-12-23 11:01:47

您可以使用 array_combine 来删除重复项

$cars = array("Volvo", "BMW", "Toyota");
array_push($cars,"BMW");

$map = array_combine($cars, $cars);

You can use array_combine for removing duplicates

$cars = array("Volvo", "BMW", "Toyota");
array_push($cars,"BMW");

$map = array_combine($cars, $cars);
她说她爱他 2024-12-23 11:01:47

Set 类。

https://www.php.net/manual/en/class。 ds-set.php

不知道什么时候出。

The Set class.

https://www.php.net/manual/en/class.ds-set.php

Don't know when will it come out.

美胚控场 2024-12-23 11:01:47

我也遇到了这个问题,所以写了一个类: https://github.com/jakewhiteley/php -set-object

正如所建议的,它确实扩展了 ArrayObject 并允许本机感觉的值插入/迭代/删除,但不在任何地方使用 array_unique() 。

实现基于 EMCA 6 JavaScript 中的 MDN JS 文档集。

I also had this problem and so have written a Class: https://github.com/jakewhiteley/php-set-object

As suggested, it does extend and ArrayObject and allow native-feeling insertion/iteration/removal of values, but without using array_unique() anywhere.

Implementation is based on the MDN JS Docs for Sets in EMCA 6 JavaScript.

缺⑴份安定 2024-12-23 11:01:47

在 Laravel 中,Collection 类中有一个方法 unique 可能会有所帮助。来自 Laravel 文档

$collection = collect([1, 1, 2, 2, 3, 4, 2]);
$unique = $collection->unique();
$unique->values()->all();
// [1, 2, 3, 4]

In Laravel there is a method unique in Collection class that may be helpful. From Laravel documentation:

$collection = collect([1, 1, 2, 2, 3, 4, 2]);
$unique = $collection->unique();
$unique->values()->all();
// [1, 2, 3, 4]
夜吻♂芭芘 2024-12-23 11:01:47

我需要在项目中使用 Set 来保存唯一的对象。为此,您可以使用 spl_object_idspl_object_hash

$mySet = [];

$mySet[spl_object_id($object)] = $object;

注意:使用 spl_object_id 时,不要将数组与 array_merge[...$array1, ...$array2] 合并,因为它们会重新排序整数键。使用 $array1 + $array2 保留现有键。

这种向数组添加对象的方式可以包装在一个类中,以确保仅添加对象并且使用 spl_object_id 添加所有对象。

I needed a Set in a project to save unique objects. To do that you can use spl_object_id or spl_object_hash.

$mySet = [];

$mySet[spl_object_id($object)] = $object;

Note: When using spl_object_id dont't merge arrays with array_merge or [...$array1, ...$array2] because those reorder integer keys. Use $array1 + $array2 to keep existing keys.

This way of adding objects to the arrays could be wrapped in a class to make sure only objects are added and all objects are added using spl_object_id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文