开发者

PHP Loop Over ONLY Different Arrays

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开发者_StackOverflow 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?


array_unique()

foreach(array_unique($array) as $key => $value)


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜