V
V
Vladislav Vasiliev2015-11-09 23:10:53
PHP
Vladislav Vasiliev, 2015-11-09 23:10:53

How to remove two identical array elements?

There is an array:

Array
(
    [0] => Array
        (
            [e-fragnet] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299129-e-fragnet-hellraisers-global-offensive-champions-league-season-2
        )

    [1] => Array
        (
            [hellraisers] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299129-e-fragnet-hellraisers-global-offensive-champions-league-season-2
        )

    [2] => Array
        (
            [e-fragnet] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299129-e-fragnet-hellraisers-global-offensive-champions-league-season-2
        )

    [3] => Array
        (
            [hellraisers] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299129-e-fragnet-hellraisers-global-offensive-champions-league-season-2
        )

    [4] => Array
        (
            [envyus] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299130-envyus-csgl-sl-i-league-starseries-xiv
        )

    [5] => Array
        (
            [virtuspro] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299123-virtuspro-mousesports-cevo-professional-season-8-finals
        )

    [6] => Array
        (
            [mousesports] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299123-virtuspro-mousesports-cevo-professional-season-8-finals
        )

    [7] => Array
        (
            [virtuspro] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299123-virtuspro-mousesports-cevo-professional-season-8-finals
        )

    [8] => Array
        (
            [mousesports] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299123-virtuspro-mousesports-cevo-professional-season-8-finals
        )

    [9] => Array
        (
            [mousesports] => http://mini.s-shot.ru/1024x2600/2048/png/?http://www.hltv.org/match/2299122-mousesports-conquest-cevo-professional-season-8-finals
        )

)

As you can see, 1 and 3, 5 and 7, as well as 6 and 8 elements are repeated in it. How can you remove those elements that have only the same key and its value? I tried to do it through array_unique, but for some reason, in the end, I get an empty array or only one element - zero[0].

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2015-11-09
@Undebraif

php.net/manual/ru/function.array-unique.php
Should help.
I wrote a comment under the post, I will duplicate the code here.

$input = []; // ваш массив
$result = [];
foreach($input as $elem)
    $result[serialize($elem)] = $elem;
$result = array_values($result); 
var_dump($result); // то что вам нужно

S
Stalker_RED, 2015-11-09
@Stalker_RED

function myFilter($inp) {
  $filter = function($carry, $item) {
    if (false === array_search($item, $carry)) $carry[] = $item;
    return $carry;
    };
    return array_reduce($inp, $filter, []);
}

Demo: codepad.viper-7.com/eG8aZL#

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question