0

I have for cycle and there I create an array of names from the list, and what I need to do is remove duplicate values, then I get this array:

Array ( [0] => Tod [1] => Admin [2] => Tod )

    $c=count($_SESSION['cart']);

$list_array = array();

    for($i=0;$i<$c;$i++){
    $id=$_SESSION['list'][$i]['id'];
    $person=get_person($id);

   $list_array[] = $person;
}
Hubert Kario
  • 21,314
  • 3
  • 24
  • 44
dojo
  • 453
  • 1
  • 10
  • 24

2 Answers2

2

Use array_unique, which returns a new array without duplicate values.

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);

Outputs

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

Check it out here.

However, you need to move $list_array outside of your for loop, and use that array in your conditionals like so,

$c=count($_SESSION['cart']);

// if this is in the loop, it will get overwritten
$list_array = array(); 

for($i=0;$i<$c;$i++){
    $id=$_SESSION['list'][$i]['id'];
    $person=get_person($id);

    // originally, you had $users_array in in_array and array_push
    if(!in_array($person, $list_array ))
        $list_array[] = $person;

}
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
Josh
  • 8,082
  • 5
  • 43
  • 41
  • also refer to this: [PHP:: array_unique for arrays inside array](http://stackoverflow.com/questions/5211900/php-array-unique-for-arrays-inside-array) – Panagiotis Mar 23 '12 at 15:10
  • I've tried to use this before, but still getting duplicated values – dojo Mar 23 '12 at 15:13
  • if I add [0] i'm getting only first letter of the name – dojo Mar 23 '12 at 15:21
  • I just realized your mistake.. Huge oversight on our parts. – Josh Mar 23 '12 at 15:28
  • if I move `$names_array = array();` out of for cycle when I try print_r it I;m get this: Array() – dojo Mar 23 '12 at 15:43
  • I also changed your conditionals. – Josh Mar 23 '12 at 15:47
  • It seems that I did as you shown, bet still getting empty array. – dojo Mar 23 '12 at 15:57
  • I suggest updating your question with your updated code so we can see the changes you've made. – Josh Mar 23 '12 at 16:04
  • Did that, it seems I've made some progressive changes, now I'm able to save array values outside of for cycle. – dojo Mar 23 '12 at 16:14
  • Excellent. Now you can either apply the `in_array` in a conditional statement, like I provided above, or use `array_unique` on `$list_array` after the `for` loop. – Josh Mar 23 '12 at 16:21
0

in_array checks for string, while you are passing an array. Same thing with array_unique - its not intended to check for multidimensional arrays. So a hotfix would be

$person=get_person($id)[0];
Artjom Kurapov
  • 6,115
  • 4
  • 32
  • 42
  • this causes an error.. get_person is a simple function with mysql query then go through for cycle to collect ar results. `function get_person($id){ $result=mysql_query("select person from list where id=$id"); mysql_set_charset("UTF8"); $row=mysql_fetch_array($result); return $row['person']; }` – dojo Mar 23 '12 at 15:22
  • `return $row['person'][0];` then? I mean if your single $person that is pushed equals to `Array ( [0] => Tod )` then obviously you cant expect a list to be cleaned and checked correctly – Artjom Kurapov Mar 23 '12 at 15:26
  • as I have mentioned above i'm also getting only first letter of the name. Its all so messed up... :/ – dojo Mar 23 '12 at 15:33