innitboy Posted October 30, 2006 Share Posted October 30, 2006 hi have a database where the 2nd column has some delimited values by a <#>:id : pies1 : red pie <#> green pie <#> blue pie <#> yellow pie2 : blue pie <#> yellow pie3 : green pie <#> blue pie <#> yellow pieI would like to get an array of all the rows in pies but i would also like to split the delimited parts into arrays and remove all the duplicatesSo i would like to end up with the array:e.g.Red pieGreen pieYellow pieBlue pieI would appreciate for any help as this is very difficult for me to get this to work.ive tried combining the arrays using explode and str_replace and strtok but nothing seems to worki have no idea how to approach this one Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/ Share on other sites More sharing options...
Zane Posted October 30, 2006 Share Posted October 30, 2006 I don't seem to follow your question all the way..are these separate columns[quote]1 : red pie <#> green pie <#> blue pie <#> yellow pie2 : blue pie <#> yellow pie3 : green pie <#> blue pie <#> yellow pie[/quote]or is it data all in one column....shown per rowMy question is how would you end up with the orderRedGreenYellowBlueI can't see that order appearing anywhere. Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116883 Share on other sites More sharing options...
innitboy Posted October 30, 2006 Author Share Posted October 30, 2006 column 1 is id with the values = 1, 2, and 3column 2 is pies which has all the delimited fields.If i query the database for rows, it just gives me the array the way each row is displayed in the database.It gives me this e.g.Array1 - red pie <#> green pie <#> blue pie <#> yellow pieArray2 - blue pie <#> yellow pieArray3 - green pie <#> blue pie <#> yellow piewhat i would like to do is get all of it in one arraye.g.Array1 - red pie <#> green pie <#> blue pie <#> yellow pie<#> blue pie <#> yellow pie <#> green pie <#> blue pie <#> yellow pieThen I would like to remove the <#> delimiter and make the array unique like thisRedGreenYellowBluemake any sense Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116892 Share on other sites More sharing options...
kenrbnsn Posted October 30, 2006 Share Posted October 30, 2006 Please post the code you're currently using.Ken Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116894 Share on other sites More sharing options...
innitboy Posted October 30, 2006 Author Share Posted October 30, 2006 hi here is my code.I am very grateful for you taking a lookI am not sure where to remove the delimiting or where to combine the arrays<?phpinclude 'library/opendb.php'; $query = "SELECT rating, productcat, productname FROM producttable WHERE productcat='".$productcat."'AND productname='".$productname."'order by rating DESC ";$result = mysql_query($query); $unique_rows = array();while (($row = mysql_fetch_array($result)) && (sizeof($unique_rows) < 10)) { if (!is_null($row) && !in_array($row, $unique_rows) && (strlen($row['rating']) < 20)) { $unique_rows[] = $row; $vowels = array( "," , "&", "-", "'", ";", "(", ")"," ", "-", "----", "---", "--"," ","_","/",":"); echo "<br>", "<a href=\"http://$cms/".str_replace($vowels, "-", $productcat)."/".str_replace($vowels, "-", $productname)."/rating_".str_replace($vowels, "_", $row['rating'])."/\">".$row['rating']."</a>\r\n"; }}?> Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116897 Share on other sites More sharing options...
kenrbnsn Posted October 30, 2006 Share Posted October 30, 2006 After the while loop completes the array unique array contains:[nobbc]Array( [0] => red pie <#> green pie <#> blue pie <#> yellow pie [1] => blue pie <#> yellow pie [2] => green pie <#> blue pie <#> yellow pie)[/nobbc]To get to your final array, there are three steps:[list][*]Use the [url=http://www.php.net/implode]implode()[/url] function to combine all the elements in the array into one string, seperated by your deliminter, ' <#> '.[*]Use the [url=http://www.php.net/explode]explode()[/url] function to create a new temporary array[*]Use the [url=http://www.php.net/array_unique()[/url] function to create an array containing just the unique value[/list]Or you can do the above steps in one statement:[code]<?php$unique = array();$unique[] = "red pie <#> green pie <#> blue pie <#> yellow pie";$unique[] = "blue pie <#> yellow pie";$unique[] = "green pie <#> blue pie <#> yellow pie";echo '<pre>' . print_r($unique,true) . '</pre>';$unique2 = array_unique(explode(' <#> ',implode(' <#> ',$unique)));echo '<pre>' . print_r($unique2,true) . '</pre>';?>[/code]The second print_r will show that the array $unique2 contains:[nobbc]Array( [0] => red pie [1] => green pie [2] => blue pie [3] => yellow pie[/nobbc]Ken Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116919 Share on other sites More sharing options...
innitboy Posted October 30, 2006 Author Share Posted October 30, 2006 I am just also wondering how to get it to work with the result of a mysql queryhere is the code i am using [code]<?phpinclude 'library/opendb.php'; $query = "SELECT productfacilities, productcat, productname FROM producttable WHERE productcat='".$productname."'AND productname='".$productname."' order by productfacilities ASC ";$result = mysql_query($query);$unique = array();// not sure what to put here$unique[] = "red pie <#> green pie <#> blue pie <#> yellow pie";$unique[] = "blue pie <#> yellow pie";$unique[] = "green pie <#> blue pie <#> yellow pie";echo '<pre>' . print_r($unique,true) . '</pre>';$unique2 = array_unique(explode(' <#> ',implode(' <#> ',$unique)));echo '<pre>' . print_r($unique2,true) . '</pre>';?>[/code]Many thanks :) Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116929 Share on other sites More sharing options...
innitboy Posted October 30, 2006 Author Share Posted October 30, 2006 please help brain is hurting ??? Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116940 Share on other sites More sharing options...
innitboy Posted October 30, 2006 Author Share Posted October 30, 2006 *bump* Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116953 Share on other sites More sharing options...
innitboy Posted October 30, 2006 Author Share Posted October 30, 2006 i know i need to change this bit$unique[] = "red pie <#> green pie <#> blue pie <#> yellow pie";$unique[] = "blue pie <#> yellow pie";$unique[] = "green pie <#> blue pie <#> yellow pie";but not sure what to change it toCheers :) Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-116983 Share on other sites More sharing options...
innitboy Posted October 30, 2006 Author Share Posted October 30, 2006 i have also tried using this and cant get it to work[code]<?phpinclude 'library/opendb.php';$query = "SELECT productfacilities FROM producttable ";$result = mysql_query($query);$array = array();while ($row = mysql_fetch_assoc($result)) {$array = array_merge($array, explode(' <#> ', $row['productfacilities'])); } $unique = array_unique($array); print_r ($unique);?> [/code] Link to comment https://forums.phpfreaks.com/topic/25612-display-delimited-array/#findComment-117007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.