Write an analog of the native function array_unique without using the built-in PHP functions for working with arrays. It is necessary to write optimally, because up to 1,000,000 items can be in the incoming array
My version:
// аналог встроенной ф-ции in_array function myInArray($needle, array $haystack, $strict = false) { foreach ($haystack as $item) { if ($strict ? ($needle === $item) : ($needle == $item)) { return true; } } return false; } function myArrayUnique(array $array) { $unique_array = array(); foreach ($array as $item) { if (!myInArray($item, $unique_array)) { $unique_array[] = $item; } } return $unique_array; } echo '<pre>'; print_r(myArrayUnique($array)); echo '</pre>'; Why is not my option (from the comment)
My method will not save keys for example in working with an array:
$arr = array("a" => "green", "red", "b" => "green", "blue", "red"); The result should be:
Array ([a] => green [0] => red [1] => blue).
And my code version will return:
Array ([0] => green [1] => red [2] => blue)