/website/source/php/array/array_unique.html
HTML | 88 lines | 84 code | 4 blank | 0 comment | 0 complexity | 0dc904ada3b9c76c4127b2dea8a1949e MD5 | raw file
- ---
- warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
- examples:
- - 'array_unique([''Kevin'',''Kevin'',''van'',''Zonneveld'',''Kevin''])'
- - 'array_unique({''a'': ''green'', 0: ''red'', ''b'': ''green'', 1: ''blue'', 2: ''red''})'
- returns:
- - '{0: ''Kevin'', 2: ''van'', 3: ''Zonneveld''}'
- - '{a: ''green'', 0: ''red'', 1: ''blue''}'
- dependencies: []
- authors:
- original by:
- - 'Carlos R. L. Rodrigues (https://www.jsfromhell.com)'
- improved by:
- - Michael Grier
- bugfixed by:
- - 'Kevin van Zonneveld (https://kvz.io)'
- - Nate
- - 'Kevin van Zonneveld (https://kvz.io)'
- - 'Brett Zamir (https://brett-zamir.me)'
- input by:
- - duncan
- - 'Brett Zamir (https://brett-zamir.me)'
- notes:
- - |-
- The second argument, sort_flags is not implemented;
- also should be sorted (asort?) first according to docs
- type: function
- layout: function
- title: PHP's array_unique in JavaScript
- description: >-
- Here’s what our current JavaScript equivalent to <a
- href="https://php.net/manual/en/function.array-unique.php">PHP's
- array_unique</a> looks like.
- function: array_unique
- category: array
- language: php
- permalink: php/array/array_unique/
- alias:
- - /functions/php/array_unique/
- - /functions/array/array_unique/
- - /php/array_unique/
- - /functions/array_unique/
- ---
- {% codeblock lang:javascript %}module.exports = function array_unique (inputArr) { // eslint-disable-line camelcase
- // discuss at: https://locutus.io/php/array_unique/
- // original by: Carlos R. L. Rodrigues (https://www.jsfromhell.com)
- // input by: duncan
- // input by: Brett Zamir (https://brett-zamir.me)
- // bugfixed by: Kevin van Zonneveld (https://kvz.io)
- // bugfixed by: Nate
- // bugfixed by: Kevin van Zonneveld (https://kvz.io)
- // bugfixed by: Brett Zamir (https://brett-zamir.me)
- // improved by: Michael Grier
- // note 1: The second argument, sort_flags is not implemented;
- // note 1: also should be sorted (asort?) first according to docs
- // example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin'])
- // returns 1: {0: 'Kevin', 2: 'van', 3: 'Zonneveld'}
- // example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'})
- // returns 2: {a: 'green', 0: 'red', 1: 'blue'}
- let key = ''
- const tmpArr2 = {}
- let val = ''
- const _arraySearch = function (needle, haystack) {
- let fkey = ''
- for (fkey in haystack) {
- if (haystack.hasOwnProperty(fkey)) {
- if ((haystack[fkey] + '') === (needle + '')) {
- return fkey
- }
- }
- }
- return false
- }
- for (key in inputArr) {
- if (inputArr.hasOwnProperty(key)) {
- val = inputArr[key]
- if (_arraySearch(val, tmpArr2) === false) {
- tmpArr2[key] = val
- }
- }
- }
- return tmpArr2
- }
- {% endcodeblock %}