Jump to content


Photo

display delimited array


  • Please log in to reply
10 replies to this topic

#1 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 06:50 PM

hi have a database where the 2nd column has some delimited values by a <#>:

id : pies

1 : red pie <#> green pie <#> blue pie <#> yellow pie
2 : blue pie <#> yellow pie
3 : green pie <#> blue pie <#> yellow pie

I 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 duplicates


So i would like to end up with the array:

e.g.

Red pie
Green pie
Yellow pie
Blue pie

I 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 work

i have no idea how to approach this one

#2 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 30 October 2006 - 06:57 PM

I don't seem to follow your question all the way..

are these separate columns

1 : red pie <#> green pie <#> blue pie <#> yellow pie
2 : blue pie <#> yellow pie
3 : green pie <#> blue pie <#> yellow pie

or is it data all in one column....shown per row

My question is how would you end up with the order
Red
Green
Yellow
Blue

I can't see that order appearing anywhere.

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#3 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 07:13 PM

column 1 is id with the values = 1, 2, and 3

column 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 pie
Array2 - blue pie <#> yellow pie
Array3 - green pie <#> blue pie <#> yellow pie

what i would like to do is get all of it in one array
e.g.

Array1 - red pie <#> green pie <#> blue pie <#> yellow pie<#>  blue pie <#> yellow pie <#> green pie <#> blue pie <#> yellow pie

Then I would like to remove the <#> delimiter and make the array unique

like this

Red
Green
Yellow
Blue

make any sense


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 October 2006 - 07:19 PM

Please post the code you're currently using.

Ken

#5 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 07:24 PM

hi here is my code.

I am very grateful for you taking a look

I am not sure where to remove the delimiting or where to combine the arrays


<?php
include '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";
   
  }

}

?>

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 October 2006 - 07:45 PM

After the while loop completes the array unique array contains:

Array
(
    [0] => red pie <#> green pie <#> blue pie <#> yellow pie
    [1] => blue pie <#> yellow pie
    [2] => green pie <#> blue pie <#> yellow pie
)

To get to your final array, there are three steps:
  • Use the implode() function to combine all the elements in the array into one string, seperated by your deliminter, ' <#> '.
  • Use the explode() 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
Or you can do the above steps in one statement:
<?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>';
?>
The second print_r will show that the array $unique2 contains:

Array
(
    [0] => red pie
    [1] => green pie
    [2] => blue pie
    [3] => yellow pie


Ken

#7 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 07:59 PM

I am just also wondering how to get it to work with the result of a mysql query

here is the code i am using

<?php
include '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>';
?>

Many thanks :)

#8 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 08:19 PM

please help brain is hurting ???

#9 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 08:57 PM

*bump*

#10 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 10:04 PM

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 to

Cheers :)

#11 innitboy

innitboy
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 30 October 2006 - 10:43 PM

i have also tried using this and cant get it to work

<?php
include '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);
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users