0

I have a single array with several of the same values. And I only want to loop over DIFFERENT values. How could I go about doing this?

Example

166-01 001;09;UO;
166-01 001;09;UO;
166-01 001;09;UO;
166-01 001;09;UO;
166-01 001;09;UO;
166-01 001;09;UO;
166-01 001;09;UO;_86
166-01 001;09;UO;_86
166-01 001;09;UO;_86
166-01 001;09;UO;_86
166-01 001;09;UO;_86
166-01 001;09;UO;_86_97
166-01 001;09;UO;_86_97
166-01 001;09;UO;_86_97
166-01 001;09;UO;_86_97_108
166-01 001;09;UO;_86_97_108
166-01 001;09;UO;_86_97_108_119
166-01 001;09;UO;_86_97_108_119

I have that in a single array, but I only want to loop for the different ones. So it would loop once for nothing, then once for _86, then once for _86_97, then once for _86_97_108, and then once for _86-97_108_119. So only loop for different key values,

or would there be a way to count the number of different keys?

3 Answers 3

3

array_unique()

foreach(array_unique($array) as $key => $value)
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are looking for array_unique(), perhaps used in conjunction with array_keys().

Comments

0

Keep track of the ones you've passed:


$passed = array();

foreach ($array as $value) {
  if (!in_array($value, $passed)) {
    $passed[] = $value;
  }
}

print_r($passed);

Or easier:

$array = array_unique($array);

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.