0
function remove_values($arr){
    $_a = array();
    while(list($key,$val) = each($arr)){
        $_a[$val] = 1;
    }
    return array_keys($_a);
}

i can't follow the above function well.i don't know what's the effect of $_a[$val] = 1; anyone can explain it to me thank you

2
  • hmmm.. Why do not you use array_unique function? Commented Apr 24, 2012 at 6:45
  • it just for learn. i see the function from the internet i want to understand it. Commented Apr 24, 2012 at 6:50

4 Answers 4

2

For the purpose of the function, Why not just use array_unique($array)?

Like this

function remove_values($arr){
    return array_keys(array_unique($arr));
}

Update:

Ok, Since you are trying to learn. Here is the explanation for the function in the post

function remove_values($arr){
    $_a = array(); //Creates a temporary array to store matching
    while(list($key,$val) = each($arr)){ //till their is item is array, loop it 
        $_a[$val] = 1; //Store in the temporary array with array's values as index, thus eliminating the duplicates
           //^ Here storing with the value as index IS THE KEY
    }
    return array_keys($_a); //return only the keys, which in fact are the unique values
}
Sign up to request clarification or add additional context in comments.

3 Comments

it just for learn. i see the function from the internet i want to understand it.
Starx , i fell that $_a[$val] = 1 the 1 can replace with other value eg: 0 . am i right? thank you
@user1345545, Sure.. it does not matter what values it is, the main thing the index.
0

Well all this does is to insert the value into the key of an array. Since all keys are unique in an array, you will remove all duplicates.

array_keys() simply gives back that array in it's ordinary form.

Example:

$arr = array('red', 'green', 'red');

$arr = remove_values($arr);

gives

array( 'red' => 1,
       'green' => 1);

which results in

array('red', 'green');

since "red" can only be a keyvalue once.

2 Comments

i fell that $_a[$val] = 1 line the 1 can replace with other value am i right? thank you
Yes, you can. It can be $_a[$val] = FALSE, $_a[$val] = TRUE or whatever. It doesn't matter since that is a dummy value.
0

Very nice function actually.

$_a[$val] = 1;

makes every element to be signed once, so returned once meaning that duplicates are removed. Than the array_keys($_a) function rebuilds and sorts the array. I liked it.

1 Comment

i am sorry i am a new learner of php. i still don't know how the above function do remove the duplicate value of an array
0

its assign the value 1 to value part. for ex:

<?php

function remove_values($arr){
    $_a = array();

    while(list($key,$val) = each($arr)){
        $_a[$val] = 1;

    }
    print_r($_a);
    return array_keys($_a);
}

$arr = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
$sed = remove_values($arr);
print_r($sed);
?>

It return two array values:

Array
(
    [apple] => 1
    [banana] => 1
    [cranberry] => 1
)
Array
(
    [0] => apple
    [1] => banana
    [2] => cranberry
)

Now tis easy for you to understand the effect of value '1'.

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.