Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array with distinct elements (like Python set)

Tags:

arrays

php

Is there a version of the PHP array class where all the elements must be distinct like, for instance, sets in Python?

like image 897
Dave Kasper Avatar asked Jul 22 '09 17:07

Dave Kasper


People also ask

How to make array elements unique in PHP?

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Note: The returned array will keep the first array item's key type.

How to get unique values from two arrays in PHP?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.

What is array unique in PHP?

The array_unique() is a built-in function in PHP and this function removes duplicate values from an array. If there are multiple elements in the array with same values then the first appearing element will be kept and all other occurrences of this element will be removed from the array.

How to check same value in array PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.


1 Answers

Nope. You could fake it by using an associative array where the keys are the elements in the "set" and the values are ignored.

like image 162
John Kugelman Avatar answered Nov 04 '22 02:11

John Kugelman