Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pulling back unique values from XML

    hi, I'm populating a drop down select box with values from an XML file. I would like it to only populate with unique values, but with the code I have at the moment it only displays the first value, can anyone spot the problem?

    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];
      }
    }
    cheers
    Steve

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Take a look at array_unique() (http://php.net/array-unique), for example:
    PHP Code:
    $array = array('one','two','three','two','one');
    $unique_array array_unique($array);
    //$unique_array == array('one','two','three'); 

  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've been playing around with this but I can't seem to get it to work in this context. how Icould I incorporate it into the drop down box?
    cheers
    Steve


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •