2

I am trying to stop an array of items from duplicating everytime the loop goes through. I would think unique array would solve my problem but it doesnt seem to be working. Is there an alternative or am I doing something wrong?

This is the array that i am parsing. For example bag shows up several times. I am trying get it to show up once.

Array
(
    [0] => bag
    [1] => wallet
    [2] => accessori
    [3] => sunglass
    [4] => clutch
    [5] => tote
)
Array
(
    [0] => handbag
    [1] => faux
    [2] => leather
    [3] => bag
    [4] => clutch
    [5] => hobo
    [6] => purs
    [7] => wallet
    [8] => tote
    [9] => messeng
)
Array
(
    [0] => bag
    [1] => book
)

This is my code.

foreach($json as $keywords){

    foreach($keywords as $keyword){

        print_r(array_unique($keyword['keywords']));

    }
}

Thank you in advance.

5
  • 1
    can you update your question with json?? Commented Mar 14, 2016 at 7:56
  • Are you trying to get unique records from all these 3 arrays? Please clarify this first. Commented Mar 14, 2016 at 7:58
  • 1
    1. array_unique() does not work recursively, you will have to implement a wrapper for that and 2. I cannot spot any key named 'keywords' in your arrays... Commented Mar 14, 2016 at 7:59
  • IF you can post your whole array then i can try by my side. Commented Mar 14, 2016 at 7:59
  • What's your expected output? E.g. where should "bag" be included in the result array? Commented Mar 14, 2016 at 8:11

2 Answers 2

1

I've wrote array_unique() function for multidimensial array
First, i've merged all the arrays, applied array_unique() and after that recomposed the result

<?php
$input  = array( array('bag',   'wallet' ),
                    array('wallet', 'purs'  ),
                    array('book',   'purs'  )       
                  );
print_r (arrayUniqueMultidimensial($input));

function arrayUniqueMultidimensial($input) {                
    $merged = array();
    foreach ($input as $subArray) {
        $merged = array_merge($merged, $subArray);
    }

    $filteredArray  = array_unique($merged, SORT_STRING);
    $segment        = array(0); // segmentation
    foreach($input as $subArray) {
        $segment[] = $segment[count($segment)-1] + count($subArray);
    }

    $recomposed = array();
    for($i=1 ; $i<count($segment) ; $i++) {
        $startIndex = $segment[$i-1];
        $endIndex    = $segment[$i] ;
        $temp        = array();
        for ($j=$startIndex ; $j<$endIndex ; $j++) {
            if (array_key_exists($j, $filteredArray)) {
                $temp[] = $filteredArray[$j];
            }
        }
        $recomposed[] = $temp;
    }
    return ($recomposed);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply merge all arrays first, then do the array_unique

    $a = array(
        'bag',
        'wallet',
        'accessori',
        'sunglass',
        'clutch',
        'tote'
    );
    $b = array(
        'handbag',
        'faux',
        'leather',
        'bag',
        'clutch',
        'hobo',
        'purs',
        'wallet',
        'tote',
        'messeng',
    );
    $c = array(
        'bag',
        'book',
    );
    $d = array_merge($a, $b, $c);
    var_dump($d);
    $u = array_unique($d);
    var_dump($u);

compare the variables $d an $u.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.