1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【初心者】PHPで配列の重複を数える

Posted at

PHPで配列の値が重複しているのかの確認

配列の中に同じ値が重複していないか(いくつ重複しているか)を確認するためには…

$n = ["HND", "NRT", "KIX", "NGO", "NGO"];
$value_count = array_count_values($n);

こちらの、array_count_values関数を使用すると…

$n = ["HND", "NRT", "KIX", "NGO", "NGO"];
$value_count = array_count_values($n);
print_r($value_count)
// 出力の結果がこちら
Array
(
    [HND] => 1
    [NRT] => 1
    [KIX] => 1
    [NGO] => 2
)

このような感じで、どの値が配列内にいくつあるかがわかるようになる。

配列内の重複の確認は、覚えておいて損なことはないんだろうなと思った。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?