1

I'v an array like this:

Array([0] => 1,[1] => 2,[2] => 3,[3] => 3,[4] => 4,[5] => 5,[6] => 5,[7] => 6,[8] => 6,[9] => 6,[10] => 7,[11] => 8,[12] => 8,[13] => 8,[14] => 8,[15] => 9,[16] => 9,[17] => 9,[18] => 9,[19] => 9)

but the results I want like this:

Array([0] => 1,[1] => 2,[3] => 3,[4] => 4,[6] => 5,[9] => 6,[10] => 7,[14] => 8,[19] => 9)

This not only eliminates the value of the same array, but the same value that appears to be the value of the index / key of the last of the same value. anyone can help me?

5
  • 2
    What have you already tried? Commented Jan 7, 2015 at 7:17
  • 1
    You can simply use array_unique($array) php.net/manual/en/function.array-unique.php Commented Jan 7, 2015 at 7:19
  • possible duplicate of How do i get unique value from an array? Commented Jan 7, 2015 at 7:20
  • dude atleast try to google also there are many similar question has been asked in stackoverflow. Commented Jan 7, 2015 at 7:21
  • array_unique is generating/eliminate the value of the same array, but I want to get the value of the index is the last.. Commented Jan 7, 2015 at 7:29

6 Answers 6

1

array_unique()

Takes an input array and returns a new array without duplicate values.

Example:

array_unique($your_array);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use array manipulation function

array_unique($arr);

Comments

0

array_unique() function through you can get a unique value of array.

<?php

$data=Array(1,2,3,3,4,5,5,6,6,6,7,8,8,8,8,9,9,9,9);
echo '<pre>';
print_r($data);
$ans=array_unique($data);
print_r($ans);
echo '</pre>';
?>

Comments

0

array_unique

<?php $arr=Array("[0]" => 1,"[1]" => 2,"[2]" => 3,"[3]" => 3,"[4]" => 4,"[5]" => 5,"[6]" => 5,"[7]" => 6,"[8]" => 6,"[9]" => 6,"[10]" => 7,"[11]" => 8,"[12]" => 8,"[13]" => 8,"[14]" => 8,"[15]" => 9,"[16]" => 9,"[17]" => 9,"[18]" => 9,"[19]" => 9);
var_dump($arr);
$newarr=array_unique($arr);
var_dump($newarr);
?>

1 Comment

yup..but the values that emerge from the first index. I want the value that appears on the index Last post of the same value.
0

Use the build in array function.

array_unique();

give you the expected the results. Check the link

Comments

0

ok, I've got the answer I want, thank you to all who have tried to help me.

$str = Array('0' => 1,'1' => 2,'2' => 3,'3' => 3,'4' => 4,'5' => 5,'6' => 5,'7' => 6,'8' => 6,'9' => 6,'10' => 7,'11' => 8,'12' => 8,'13' => 8,'14' => 8,'15' => 9,'16' => 9,'17' => 9,'18' => 9,'19' => 9);

$flag = 0;
foreach($str as $key=>$value)
{
   if($flag == $value)
   {
      unset($str[$key-1]);
   }
   $flag = $value;
 }
 echo "<pre>";
 print_r($str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.