0

I am a newbie in this and I have read lots of stuff about this matter (including some topics here), before starting this topic, but I do not quite get it yet, so I will ask for some help (if it is possible) :)

So, in the column that I want to print I have values like this on every row:

value1|value2|value5|value12|value25

value3|value5|value12|value14|value26|value32|value55

value1|value2|value14|value26|value31

The number of rows can be 3 or 1500+... So I want to merge the arrays and print those values sorted and without duplicates: value1, value2, value3, value5, value12, etc...

I have tried to explode the arrays, but I could not find out how to assign a variable to every array and merge them and all I have done is to print all values:

foreach ($rows as $areas) {
    foreach (explode('|', $areas->value) as $area) {
        var_dump($area);
    }
}

Afterwards I have read somewhere this will be very slow if I have many rows (and I am going to have thousands), so I am stuck here and I do not know what else I could do...

I will appreciate any help and direction that you can give me, because it is too hard for me and I can not do it without help

Thank you in advance

Joro
  • 3
  • 2
  • Possible duplicate of [How to remove duplicate values from an array in PHP](http://stackoverflow.com/questions/307650/how-to-remove-duplicate-values-from-an-array-in-php) – Machavity Oct 15 '15 at 12:13

2 Answers2

0

You can store each value of your exploded string as key (if it's not an object nor array), it store only unique values. Then you have to just use array_keys() to get keys and sort returned array:

$rows = array(
    'value1|value2|value5|value12|value25',
    'value3|value5|value12|value14|value26|value32|value55',
    'value1|value2|value14|value26|value31'
);

$results = array();
foreach ($rows as $row) {
    $items = explode('|', $row);
    foreach ($items as $item) {
        $results[$item] = 0;
    }
}
$results = array_keys($results);
sort($results, SORT_NATURAL);

Live demo on eval.in

marian0
  • 3,336
  • 3
  • 27
  • 37
  • `$results[$item] = 0;` is ugly, better use `$results[] = $item;` with `array_unique()`. And I think the `sort()` function isn't working because all values are 0. – Roy Oct 15 '15 at 12:22
  • @Roy That's why I'm using `array_keys()` before sort. – marian0 Oct 15 '15 at 12:23
  • @Roy and maybe it's ugly, but is more efficient than using after all `array_unique()`. – marian0 Oct 15 '15 at 12:28
0

There are two ways of doing this:

<?php

$str = 'value1|value2|value5|value12|value25';
$str1 = 'value3|value5|value12|value14|value26|value32|value55';
$str2 = 'value1|value2|value14|value26|value31';


//-- Method 1: Concat and make a single string and then explode and make a single array
$finalString = $str . '|' . $str1 . '|' . $str2;
print_r(array_unique(explode('|', $finalString)));

//-- Method 2: explode first and then merge into a single array
$strArr = explode('|', $str);
$strArr1 = explode('|', $str1);
$strArr2 = explode('|', $str2);
print_r(array_unique(array_merge($strArr, $strArr1, $strArr2)));
Ashish Choudhary
  • 2,004
  • 1
  • 18
  • 26