Solved

php csv value strings into merged array and remove duplicates

Posted on 2014-09-22
9
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

738 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