0

I´m facing this issue, probably this is dumb... but here we go:

I have a regular foreach:

 foreach $values as $values => $key {
   $test[] = $key;
 }

of course test is defined outside the loop.

Now, this returns something like this:

 array(6) {
 [0]=> int(10)
 [1]=> int(16)
 [2]=> int(10)
 [3]=> int(16)
 [4]=> int(10)
 [5]=> int(16)
 [6]=> int(10)
 }

However, I need the repeated values to be ignored so test would be like this:

 array(2) {
 [0]=> int(10)
 [1]=> int(16)
 }

And new values are only added if and only if, it is not repeated. I already tried array_unique and array_values however I'm not use If I am implementing them bad or what is the issue...

Any help will be appreciated.

Best Regards.

5
  • It's unclear to me what you want Commented Apr 29, 2014 at 13:44
  • 3
    What was wrong when using array_unique? Also see in_array. Commented Apr 29, 2014 at 13:45
  • it reuturns null for some reason Commented Apr 29, 2014 at 13:47
  • 1
    You're overriding $values in your loop, after the loop $values becomes the last element in the array, not the array itself. Commented Apr 29, 2014 at 13:52
  • 1
    Is it just me or is foreach $values as $values => $key the worst possible var names ever? Commented Apr 29, 2014 at 13:54

5 Answers 5

2

If you only want to add the value if it doesn't exist, use in_array()

 $test=array();
    // organize the array by cusip
    foreach ($values as $values => $key) { 
            if(!in_array($key, $test)){
            $test[]=$value;
            }    
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Why not use array_unique()?

Consider this an example:

$values = array_unique($values);

or if you do not want to use array_unique(), you can do it in another way:

foreach ($values as $index => $element) {
    if(!in_array($element, $values)) {
        $test[] = $element;
    } 
}

1 Comment

yeah but for some reason this returns null on the key values
1

How about assigning the array using array_unique:

<?php
    $x = array
    (
        0 => 'int(10)',
        1 => 'int(16)',
        2 => 'int(10)',
        3 => 'int(16)',
        4 => 'int(10)',
        5 => 'int(16)',
        6 => 'int(10)'
    );

    $x = array_unique($x);
    print_r($x);
?>

This will output

Array
(
    [0] => int(10)
    [1] => int(16)
)

1 Comment

now this worked, thanks a ton, it made the trick, i knew it was something silly, i was applying array_unique into the foreach to the $w.
1

If array_unique somehow won't work for you, you could use:

<?php
foreach( $values as $values => $key ){
   if( !in_array($key, $test) )
      $test[] = $key;
}
?>

Comments

0

Here your array is

$test = array(6) {
 [0]=> int(10)
 [1]=> int(16)
 [2]=> int(10)
 [3]=> int(16)
 [4]=> int(10)
 [5]=> int(16)
 [6]=> int(10)
 }

You can use array_unique($test) which will removes duplicate values.

1 Comment

Could you add some explanation and maybe an example? As it stands this answer is too short.

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.