View Full Version : array question..
lowridertj
Jun 19th 2009, 11:58 am
If I have an array
DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4
and this is being transferred to a text box.
How can I remove the duplicate entries?
Example code:
//While the URL is passing the cc string
// DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4
$cc = $_GET['cc'];
<input name="ccc" type="text" value="<?php echo "$cc";?>"/>
Any and all help with this would be greatly appreciated. I been racking my head around and think I have been over thinking this for to long that I cant figure it out.
TJ
webhed21
Jun 19th 2009, 12:15 pm
use the array_unique($array) function which is built into php. It removes all duplicate values from an array.
$cc = array_unique($cc);
lowridertj
Jun 19th 2009, 1:19 pm
use the array_unique($array) function which is built into php. It removes all duplicate values from an array.
$cc = array_unique($cc);
tried it.
//While the URL is passing the cc string
// DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4
$cc = "DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4";
print_r($cc);
echo "<BR>";
$cc = trim($cc);
print_r($cc);
echo "<BR>";
$c = explode(" , ", $cc);
print_r($cc);
echo "<BR>";
$cc = array_unique($cc);
print_r($cc);
doesnt work.
Brewster
Jun 19th 2009, 1:56 pm
It does work. The real issue here is that you are not using descriptive variable names which makes debugging code a nightmare. Try this:
<pre><?php
$myString = "DGN,DGN,TEST,TEST3,TEST3,TEST4,TEST4";
print_r($myString);
echo "<BR>";
$myString = trim($myString);
print_r($myString);
echo "<BR>";
$myArray = explode(",", $myString);
print_r($myArray);
echo "<BR>";
$myArray = array_unique($myArray);
print_r($myArray);
?></pre>
Brew
lowridertj
Jun 19th 2009, 2:30 pm
It does work. The real issue here is that you are not using descriptive variable names which makes debugging code a nightmare. Try this:
<pre><?php
$myString = "DGN,DGN,TEST,TEST3,TEST3,TEST4,TEST4";
print_r($myString);
echo "<BR>";
$myString = trim($myString);
print_r($myString);
echo "<BR>";
$myArray = explode(",", $myString);
print_r($myArray);
echo "<BR>";
$myArray = array_unique($myArray);
print_r($myArray);
?></pre>
Brew
thank you.
Now while it shows
Array
(
[0] => DGN
[1] => DGN
[2] => TEST
[3] => TEST3
[4] => TEST3
[5] => TEST4
[6] => TEST4
)
Array
(
[0] => DGN
[2] => TEST
[3] => TEST3
[5] => TEST4
)
How can I get the second set to display together as
DGN, TEST, TEST3, TEST4
shown as $cc = DGN, TEST, TEST3, TEST4;
to be able to display it in the form text area?
And again I do appreciate all the help guys and gals.
TJ
Brewster
Jun 19th 2009, 2:39 pm
I love easy questions :) Use the function implode()
implode(', ', $myArray)
Brew
lowridertj
Jun 19th 2009, 3:30 pm
much appreciated..
.
TJ
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.