0

I want to use unique array in for loop. Please see my code.

Array ( [0] => Pritesh [1] => Pritesh [2] => Nilesh )

Now i have used "UNIQUE" function for fetch unique value

 Array ( [0] => Pritesh [2] => Nilesh ) 

But Now issue is when i use for loop for that like

for($p = 0; $p < count($unique); $p++)
    {   echo $uniques;
        echo  '<option value="'.$unique[$p].'">'.$unique[$p].'</option>';
    }   

So second value showing blank because array count is 2 and Key is 0,2 .

How i use this.

Pritesh Mahajan
  • 4,974
  • 8
  • 39
  • 64

6 Answers6

4

Do like this before you pass into the loop..

$yourarray = array_values(array_unique($repeated_values_array));

Now make use of $yourarray in the for loop.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • `array_values` returns yet another array. This code may be a one-liner, but it takes an array, creates a new, unique-values-only array,and then creates a third array with only the unique values. `sort` resets the array's keys without creating the unique array anew. – Elias Van Ootegem Dec 19 '13 at 07:28
  • `Sorting` is **not** the right thing to do. What if the values are to be maintained in the same position ? – Shankar Narayana Damodaran Dec 19 '13 at 07:37
  • If that is the case, sorting isn't right, true (unless you use `usort` with `function keepOrder($a, $b) { return 0; }`, but that's silly). In this case, OP didn't mention he cared about the order, so I didn't assume the order of elements was relevant. – Elias Van Ootegem Dec 19 '13 at 07:40
  • I've made some edits to my answer, suggesting your answer if OP wants to guarantee key-integrity and not to change the order of elements in the array. I've also added some silly solutions, for fun – Elias Van Ootegem Dec 19 '13 at 07:58
2
foreach($uniques as $key => $unique) {
  echo '<option value="'.$unique.'">'.$unique.'</option>';
}

As stated in the comment, you do not need to assign a variable for the key, if you don't need it. (I just used is as a handy reference). So you can simply do this:

foreach($uniques as $unique) {
  echo '<option value="'.$unique.'">'.$unique.'</option>';
}
Frederik Wordenskjold
  • 10,031
  • 6
  • 38
  • 57
  • why bother `foreach($uniques as $key => $unique)`, assigning 2 variables on each iteration, if you're not going to use `$key`? `foreach($uniques as $unique)` is shorter to write, marginally faster and iterates only of that which you need: the values – Elias Van Ootegem Dec 19 '13 at 07:37
  • 1
    I thought it would be a good idea to include the key variable, as OP probably did not know about this syntax. Performance is not the issue here. But you're certainly right. I've edited my answer to include both constructs. – Frederik Wordenskjold Dec 19 '13 at 08:48
  • Now, since I'm on a pedantic-streak: the most efficient `echo` doesn't concatenate strings, it comma-separates them. [I've given a lengthy explanation on the matter here](http://stackoverflow.com/questions/20596098/php-closing-tag-deletes-the-line-feed/20596645#20596645) – Elias Van Ootegem Dec 19 '13 at 12:03
2

Please, read the manual. array_unique doesn't reset the array keys. Besides, a for loop is all well and good, but foreach is a language construct better suited to deal with arrays (and objects, for that matter).
That said, the fastest way to reset the array keys after array_unique has been applied is to sort the array:

$a = array( 'Pritesh','Pritesh','Nilesh' );
$b = array_unique($a);
sort($b);
var_dump($b);

This results in:

array(2) {
  [0]=>
  string(6) "Nilesh"
  [1]=>
  string(7) "Pritesh"
}

Which is suitable for both for and foreach.

Edit:
As is often the case in programming TMTOWTDI (There's more than one way to do it). Here's a couple of approaches, ranging from valid to absurd:

Quick:

foreach($uniques as $unique) echo '<option value="', $unique, '">', $unique,'</option>';

Yes those are comma's, comma's + echo is actually faster (~20-30%) than concatenation
When to use: whenever you feel like it, it doesn't change the $uniques array (so it's still inconcistently indexed), but it's quick and easy to read.

Fix $uniques indexing:

sort($uniques);
foreach($uniques as $unique) echo '';//same deal

The difference with the previous version is that you can replace foreach with for here, too, and that the $uniques array will be properly indexed again. The order of the elements may have changed.
If the order of the elements is important, and you want a neatly indexed array, use:

$uniques = array_values($uniques);//as Shankar Damodaran suggested

Use when your use-case fits the very specific and unlikely scenario described above.
or, and things are getting absurd from here on end:

function keepOrder($a, $b)
{
    return 0;
}
usort($uniques, 'keepOrder);

Use: never

Since we are taking it rather far in terms of over-engineering a solution, let's take it to the extend where the solution becomes a problem all over again (Warning, the following code may cause your eyes to bleed):

//get the highest key in array, add 1
$max = max(array_keys($uniques)) + 1;
for($i=0;$i<$max;++$i) if (isset($uniques[$i])) echo $unique;

I've deliberately made the code above harder to read, to avoid anyone using it.
When to use: use when you are either clinically insane, or want to see a co-worker being taken away in a straitjacket, shouting "The horror, the horror".
Seriously: don't use it.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1

Use the foreach function of PHP

 foreach($unique as $val)
 {   
      echo $val;
      echo  '<option value="'.$val.'">'.$val.'</option>';
 }  
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
1

Use foreach instead of for loop

if you need your key as well, do this:

foreach ($unique as $key=>$value)

where $key will be the array key.

Jonathan Chow
  • 703
  • 7
  • 29
0

It seems that what you need is called "rebase array keys". As discussed in this post: Rebase array keys

So use

$array = array_values($array);
Community
  • 1
  • 1
Oskars Pakers
  • 699
  • 3
  • 11