Jump to content

Store Non-repetitive Items

- - - - -

  • Please log in to reply
2 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hi
How to Save Non-repetitive Items in another Array.


$arr = Array

        (

            "0" => 			"15:30",

            "1" => 			"20:45",

            "2" => 			"20:45",

            "3" => 			"20:45",

            "4" => 			"20:45",

            "5" => 			"21:00",

            "6" => 			"21:00",

            "7" => 			"21:00",

            "8" => 			"14:30",

            "9" => 			"18:00",

            "10" => 			"20:00"

        );


Thank you ^^

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You would be looking for array_unique function, many array functions are listed here: PHP: array_unique - Manual

<?php
$arr = Array
        (
            "0" =>             "15:30",
            "1" =>             "20:45",
            "2" =>             "20:45",
            "3" =>             "20:45",
            "4" =>             "20:45",
            "5" =>             "21:00",
            "6" =>             "21:00",
            "7" =>             "21:00",
            "8" =>             "14:30",
            "9" =>             "18:00",
            "10" =>             "20:00"
        ); 
        
print_r(array_unique($arr));
Result:
Array
(
    [0] => 15:30
    [1] => 20:45
    [5] => 21:00
    [8] => 14:30
    [9] => 18:00
    [10] => 20:00
)
You can filter the array through sort, if the array keys are important to you (they will be shuffled)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
If you were trying to make your own logic, you'd loop through the array, creating a new array as you go and check if it already exists or not. This has identical output as Alexander's script.

<?PHP
function array_unique2($items) {
    $newItems = array();
    foreach($items as $item) {
        $add = TRUE;
        foreach($newItems as $nItem) {
            // Could also use "in_array" but that probably does the same thing
            if($nItem == $item) {
                $add = FALSE;
                break;
            }
        }
        if($add) {
            $newItems[] = $item;
        }
    }
    return $newItems;
}
$arr = Array
        (
            "0" =>             "15:30",
            "1" =>             "20:45",
            "2" =>             "20:45",
            "3" =>             "20:45",
            "4" =>             "20:45",
            "5" =>             "21:00",
            "6" =>             "21:00",
            "7" =>             "21:00",
            "8" =>             "14:30",
            "9" =>             "18:00",
            "10" =>             "20:00"
        );
print_r(array_unique2($arr));





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users