Here is my array:

$myarray = "B12@A10@C11@C11@B12@F10@B12";

Is there a function I can use that will remove all duplicates and just keep 1 instance if there is a duplicate. So in the above example my new array would be:

$new_myarray = "B12@A10@C11@F10";

    ... That doesn't look like a array... Does it?

    Is that "array" of yours imported? (e.g. JavaScript).

    Either way, it looks like a string...

      That is not an array, that is a string. What you could do is use [man]explode/man to turn it into an array, [man]array_unique/man to remove duplicates(), then [man]implode/man to turn it back to a string.

      For example:

      $str = "B12@A10@C11@C11@B12@F10@B12";
      $array = explode('@', $str);
      $array = array_unique($array);
      $str = implode('@', $array);

        Btw, I did a Goggle search for your title (adding +PHP): http://www.zend.com/tips/tips.php?id=18&single=1 (if you feel lucky)

        UPDATE: hah, I knew there was a function, just didn't remember the name. The link is from 2000 (I suppose that it could be useful if you're running an older PHP version).

          The link is from 2000 (I suppose that it could be useful if you're running an older PHP version).

          I am afraid that that code is not helpful even for older versions of PHP. It uses count() with a second parameter. This parameter was introduced in PHP 4.2.0. array_unique(), on the other hand, has been around since PHP 4.0.1.

            I know it is a string, I just left out the part where I explode the string into an array. Anyway, thanks for the help but I figured it out and coded my own function.

            Thanks

              Anyway, thanks for the help but I figured it out and coded my own function.

              Unless you are doing it for efficiency, do not do that. Use what is available as it will also make your code more immediately understandable to a reader new to your code but familiar with PHP's API.

                "I am afraid that that code is not helpful even for older versions of PHP. It uses count() with a second parameter. This parameter was introduced in PHP 4.2.0. array_unique(), on the other hand, has been around since PHP 4.0.1."

                Interesting, I didn't know that. I thought the link was an alternative before the array_unique version came; wonder why it still hits high on Goggle...

                I was wondering one thing about your last post laserlight:

                "Unless you are doing it for efficiency, do not do that. "

                Are you saying the the PHP built in functions are not optimal? i.e. one cold do a better home brew alternative?

                  Are you saying the the PHP built in functions are not optimal? i.e. one cold do a better home brew alternative?

                  In some cases, yes, as those built-in functions are rather generic. A hand crafted version can beat such a function since it is better able to make use of the specifics of the situation.

                  For example, I wrote and compared this code with array_unique() :

                  function arrayUnique($array)
                  {
                      if (!empty($array))
                      {
                          sort($array);
                          $result = array($array[0]);
                          for ($i = 0, $j = 0; $i < count($array); ++$i)
                          {
                              if ($array[$i] != $result[$j])
                              {
                                  $result[] = $array[$i];
                                  ++$j;
                              }
                          }
                          return $result;
                      }
                      else
                      {
                          return $array;
                      }
                  }

                  My code was about 20% faster when tested on my machine, but it only handles numerically indexed arrays and it distorts the original order of the elements by the sorting.

                    Interesting, and good to know (it will take a while before I can make use of this 😉)

                    Thanks.

                      Write a Reply...