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
https://forums.phpfreaks.com/topic/25612-display-delimited-array/
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.
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
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";
   
  }

}

?>
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
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 :)
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.