Solved

php csv value strings into merged array and remove duplicates

Posted on 2014-09-22
9
501 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…
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.

705 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