Solved

php csv value strings into merged array and remove duplicates

Posted on 2014-09-22
9
486 Views
Last Modified: 2014-09-24
I am querying a table which contains csv string values in each returned row.
I need to end up with a list of only individual unique values from the csv values.
As I see it... implode(', ', $row['value'), then array_merge() to get a single array and finally array_unique to remove duplicates - I've tried all this til I'm going in circles, but can't get it right.

using this in modx snippet:
    $res = $modx->query("SELECT DISTINCT `value` FROM `modx_site_tmplvar_contentvalues` WHERE `tmplvarid` = 2 ORDER BY `value`");
	while($row = $res->fetch(PDO::FETCH_ASSOC) ){
		echo $row['value']."<br/>";
		//$allvalues[] = array_merge($allvalues, $rowvalues);
		//$output = array_merge($items_like, $other_likes);
		//$allvalues[] .= explode(',',$row['value']);
	}
	//$allvalues = array_unique($allvalues);
	//print_r($allvalues);
	//print_r($allvalues);
	//explode(',',$row['value'])
	//return print_r($output);
	//print_r(explode(',',$row));
	//print_r(array_unique($output));

Open in new window


I get this if I just echo all the rows to test (as above)

Driving
Driving, HGV Driving
Driving, HGV Driving, Motorcross, Motorcycle, Nigh...
Driving, HGV Driving, Motorcycle
Driving, HGV Driving, Motorcycle, Night Driving
Driving, HGV Driving, Night Driving
Driving, Motorcycle, HGV Driving
Driving,HGV Driving
HGV Driving
HGV Driving, Driving
HGV Driving, Driving, Motorcycle
HGV Driving, Motorcross, Motorcycle
HGV Driving, Motorcycle
Motorcross
Motorcross, Motorcycle
Motorcross, Motorcycle, Night Driving
Motorcycle
Motorcycle, Driving
Motorcycle, Driving, HGV Driving
Motorcycle, Driving, HGV Driving, Motorcross
Motorcycle, Driving, Night Driving, HGV Driving
Motorcycle, HGV Driving, Driving
Motorcycle, HGV Driving, Driving, Motorcross
Motorcycle, Motorcross

Help please : )

thanks
0
Comment
Question by:freshwaterwest
  • 5
  • 4
9 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40337379
What do you want to keep from all of those rows?  An example would be helpful.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40337404
You may be able to exploit the nature of the PHP associative array, where array elements with duplicate keys overwrite each other.  Example here.
http://iconoun.com/demo/temp_freshwaterwest.php

<?php // demo/temp_freshwaterwest.php
error_reporting(E_ALL);

// TEST DATA FROM THE QUERY
$arr = array
( 'Driving'
, 'Driving, HGV Driving'
, 'Driving, HGV Driving, Motorcross, Motorcycle, Nigh...'
, 'Driving, HGV Driving, Motorcycle'
, 'Driving, HGV Driving, Motorcycle, Night Driving'
, 'Driving, HGV Driving, Night Driving'
, 'Driving, Motorcycle, HGV Driving'
, 'Driving,HGV Driving'
, 'HGV Driving'
, 'HGV Driving, Driving'
, 'HGV Driving, Driving, Motorcycle'
, 'HGV Driving, Motorcross, Motorcycle'
, 'HGV Driving, Motorcycle'
, 'Motorcross'
, 'Motorcross, Motorcycle'
, 'Motorcross, Motorcycle, Night Driving'
, 'Motorcycle'
, 'Motorcycle, Driving'
, 'Motorcycle, Driving, HGV Driving'
, 'Motorcycle, Driving, HGV Driving, Motorcross'
, 'Motorcycle, Driving, Night Driving, HGV Driving'
, 'Motorcycle, HGV Driving, Driving'
, 'Motorcycle, HGV Driving, Driving, Motorcross'
, 'Motorcycle, Motorcross'
)
;

// MAKE AN ASSOCIATIVE ARRAY IN $out
$out = array();

// WITH EACH OF THE RETURN ROWS FROM THE QUERY
foreach ($arr as $str)
{
    // OPEN UP THE CSV STRING INTO AN ARRAY
    $sub = explode(',', $str);

    // WITH EACH OF THE POSITIONS IN THE CSV
    foreach ($sub as $val)
    {
        // NORMALIZE THE DATA
        $val = trim($val);

        // USE IT FOR BOTH KEY AND VALUE
        $out[$val] = $val;
    }
}

// SHOW THE WORK PRODUCT
print_r($out);

Open in new window

0
 

Author Comment

by:freshwaterwest
ID: 40341756
Hi Ray, thanks for your replies

sorry for the delay in getting back to you on this - what I'm aiming to output from the above for example would be:

Driving, HGV Driving, Motorcross, Motorcycle, Night Driving, Motorcross - i.e. just one of each different value.

(My actual data has more but this demonstrates the example)

many thanks
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40341764
Check the script I posted earlier: http://iconoun.com/demo/temp_freshwaterwest.php

If you want output in the form of a string variable with comma separators, you can use implode() to process the $out array.
0
 

Author Comment

by:freshwaterwest
ID: 40341812
Hi Ray,

I did try this and checked out your script thanks, I'm trying to achieve this output (in array form):

Array ( [0] => Driving [1] => HGV Driving [2] => Motorcross [3] => Motorcycle [4] => Night Driving [5] => Motorcross )

As you can see I only need a simple array - not any kind of multidimension.

many thanks
0
 

Author Comment

by:freshwaterwest
ID: 40341819
Maybe I should try concatenate all the rows into one then remove duplicates?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40341933
The script I posted does not create a multidimensional array.  It creates a one-dimensional array.  Here is the output, visualized with "view source" in the browser.
Array
(
    [Driving] => Driving
    [HGV Driving] => HGV Driving
    [Motorcross] => Motorcross
    [Motorcycle] => Motorcycle
    [Nigh...] => Nigh...
    [Night Driving] => Night Driving
)

Open in new window


If you don't need the array keys, just ignore them!  I can't imagine why you might want to remove the array keys, but you can do that, too.
http://php.net/manual/en/function.array-values.php
0
 

Author Closing Comment

by:freshwaterwest
ID: 40341976
thanks Ray, spot on...

sorry for being a bit slow on the uptake!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40342066
Thanks for the points and thanks for using E-E! ~Ray
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question