Jump to content

display delimited array


innitboy

Recommended Posts

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
Link to comment
Share on other sites

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

are these separate columns
[quote]
1 : red pie <#> green pie <#> blue pie <#> yellow pie
2 : blue pie <#> yellow pie
3 : green pie <#> blue pie <#> yellow pie
[/quote]
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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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";
   
  }

}

?>
Link to comment
Share on other sites

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
Share on other sites

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

[code]<?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>';
?>
[/code]

Many thanks :)
Link to comment
Share on other sites

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

[code]<?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);
?> [/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.