0

Here, I have the following variable $countries_all:

Array ( 
  [0] => stdClass Object 
      ( 
          [countries] => India,Afghanestan,Japan,South Africa
      ) 
  [1] => stdClass Object 
      ( 
          [countries] => Singapore,South Africa,India,Pakistan
      )
  [2] => stdClass Object 
      ( 
          [countries] => Japan,Australia,India
      ) 
)

But i need to create second array $countries containing only unique values:

Array ( 
  [0] => stdClass Object 
      ( 
          [countries] => India
      )
  [1] => stdClass Object 
      ( 
          [countries] => Afghanestan
      )
  [2] => stdClass Object 
      ( 
          [countries] => Japan
      )
  [3] => stdClass Object 
      ( 
          [countries] => South Africa
      )
  [4] => stdClass Object 
      ( 
          [countries] => Singapore
      )
  [5] => stdClass Object 
      ( 
          [countries] => Pakistan
      )
  [6] => stdClass Object 
      ( 
          [countries] => Japan
      )
  [7] => stdClass Object 
      ( 
          [countries] => Australia
      ) 
)

For splitting by (,) I have used the below method

foreach ($countries_all as $list){
    $countrieslist=explode(',', $list->countries);
    foreach ($countrieslist as $uni_countries){
        echo $uni_countries;
    }
}

but how to get unique values?

2
  • Where do you get $countries from? Is that supposed to be $uni_countries? Commented Sep 28, 2013 at 10:10
  • ha sorry it is $uni_countries only Commented Sep 28, 2013 at 10:11

6 Answers 6

2

The array_unique function is what you are looking for.

$countries = array();
foreach ($countries_all as $list){
    $countrieslist=explode(',', $list->countries);
    foreach ($countrieslist as $country){
        $countries[] = $country;
    }
}
$countries = array_unique($countries);
Sign up to request clarification or add additional context in comments.

Comments

1
$newlist = array();
foreach ($countries_list as $list){
    $countrieslist=explode(',', $list->countries);
    foreach ($countrieslist as $uni_countries){
        echo $uni_countries;
        $newlist[] = $uni_countries){
    }
}
array_unique($newlist);

Comments

1
$commasplit = function ($list) { return explode(',', $list->countries); };
$countrieslist = array_unique(call_user_func_array('array_merge', array_map($commasplit, $countries_list)));

Comments

1

You may try this

$countrieslist = array();
foreach ($countries_all as $list){
    $countrieslist[] = explode(',', $list->countries);
}
$array = array_unique(array_reduce($countrieslist,'array_merge', array()));

An Example.

Comments

0

Try this:

$uniqueCountries = array();

foreach ($countries_list as $list){
    $countrieslist=explode(',', $list->countries);
    foreach ($countrieslist as $country){
        if (!in_array($country, $uniqueCountries)) {
            //adding to list
            $uniqueCountries[] = $country
        } else {
             echo $country, ' already in list', PHP_EOL;
        }
    }
}

$uniqueCoutries contains all found coutries, without duplicates

Comments

0

Instead of asking array_unique or in_array() to perform extra cycles to weed out the duplicates, don't let the duplicate values in in the first place. You can rely on PHP's refusal to have duplicate keys in the same level of an array.

By assigning the single country value not only as the object property, but also as the first level key, you ensure that there be no duplication even if the same country is encountered again while iterating.

This technique will perform faster than earlier posted techniques on this page.

If you don't want the first level keys after iterating, you can re-index the array with array_values().

Code: (Demo)

$result = [];
foreach ($array as $obj) {
    foreach (explode(',', $obj->countries) as $country) {
        $result[$country] = (object) ['countries' => $country];
    }
}
var_export(array_values($result));

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.