![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New Coder ![]() Join Date: Apr 2005
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Problem getting unique values from an XML file
Hi,
I'm using an XML file to populate a form select box. the XML file has numerous values that I'm populating the form with, some of which are repeating themselves. I want to have unique values in the form so no values are repeated. for example In XML doc Code:
<parent> <child>1</child> <child>1</child> <child>2</child> <child>1</child> </parent> Code:
...
echo "<option value = ''>Please pick a number</option> ";
foreach ($entries as $entry)
{
echo "<option value = '{$entry->nodeValue}'>{$entry->nodeValue}</option>";
}
...
thanks in advance for any help you may be able to give! Steve |
|
|
|
|
|
PM User | #2 |
|
Senior Coder ![]() Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
This is more of a PHP question than XML, IMO. But even knowing close to nothing about PHP I'll take a shot. :)
Code:
$i = 0;
foreach ($entries as $entry) {
$arr[$i++] = $entry->nodeValue;
}
sort($arr);
$last_value = -1;
for ($j = 0; $j < $i; $j++) {
if ($arr[$j] != $last_value) {
echo "<option value = '{$arr[$j]}'>{$arr[$j]}</option>";
$last_value = $arr[$j];
}
}
david_kw Last edited by david_kw; 01-06-2007 at 10:21 PM.. Reason: saw a couple missing $s |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|