www.webdeveloper.com
Results 1 to 7 of 7

Thread: array_unique(); not working

  1. #1
    Join Date
    Apr 2003
    Posts
    137

    array_unique(); not working

    Well, actually yes it is working.
    I just discovered that I can get it working using
    " print_r " but it will not work with " echo ".

    I haven't used .. print_r that much and I'm
    not sure of the difference other than .. print_r ..
    is a function.

    If I remember correctly, "C++" has a similar function
    and was used along with formatting text,
    but I can't seem to find a reference for the function.

    example code:
    PHP Code:
    <?php
    $names 
    = array(
    "1110_nzd_jpy"
    "1110_nzd_jpy"
    "1110_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_nzd_jpy"
    "1107_aud_usd"
    "1107_nzd_jpy");

    print_rarray_values($names));
    echo 
    "<br>";
    $names_unique = array();
    $names_unique array_unique($names);
    print_r($names_unique);
    ?>
    My question would be, why does it
    not work with " echo "?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    How are you using it with echo? If you just do echo $arrayName;, all you will get is something telling you it's an array. To actually see the content, you would have to loop through the array, something like:
    PHP Code:
    $array array_unique($array);
    foreach(
    $array as $value) {
       echo 
    "$value<br />\n";

    (Sorry if you already know this and your question means something else entirely -- in which case I need more info on what you mean by "not working". )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2003
    Posts
    137
    I take it back, it's not working,
    I've checked and checked for errors,
    changed it to print_r...

    PHP Code:
    <table><tr><th>Events</th></tr>
    <?php
    $i 
    0;
    while(
    $i count($name_unique))
      { 
    ?><tr><td><?php print_r$name_unique[$i]); ?></td></tr>
      <?php $i++; } ?>

    </table>

  4. #4
    Join Date
    Apr 2003
    Posts
    137
    duh,
    I'm so embarrassed

    the array_unique does not change the key#

  5. #5
    Join Date
    Apr 2003
    Posts
    137
    thx Nodog,

    Now I'm seeing an advantage for using
    ... foreach() ...

    My looping was correct, however the
    array_unique leaves the index keys
    with the same value as the original
    array. therefore my looping
    while($i=0;$i<count(array);$i++){}
    would not work.
    The foreach() would.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    Ah...I see what you mean. I almost always use foreach(), in part just for that reason. But if you ever need the indexes to be renumbered, you could do:
    PHP Code:
    $array array_values(array_unique($array)); 
    (And of course you don't want to do that with an associative array, in most cases anyway. )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Apr 2003
    Posts
    137
    Quote Originally Posted by NogDog View Post
    Ah...I see what you mean. I almost always use foreach(), in part just for that reason. But if you ever need the indexes to be renumbered, you could do:
    PHP Code:
    $array array_values(array_unique($array)); 
    (And of course you don't want to do that with an associative array, in most cases anyway. )
    thx NogDog,

    I actually began searching for a way to do that
    but I realized that if I wanted to refer to a specific
    index it would mess up my database search,
    however, in some instances I might need to
    do that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles