Solved

php csv value strings into merged array and remove duplicates

Posted on 2014-09-22
9
463 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 108

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 108

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
 
LVL 108

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 108

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 108

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to dynamically set the form action using jQuery.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now