Solved

php csv value strings into merged array and remove duplicates

Posted on 2014-09-22
9
474 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

16 Experts available now in Live!

Get 1:1 Help Now