Help us understand the problem. What is going on with this article?

php 配列 重複した要素の削除 array_unique()

配列の重複要素を削除

array_unique()

qiita.php
$array = ["aaaa","wwww","eeee","aaaa","tttt","WWWW","eeee"];
$array = (array_unique($array));
//Array ( [0] => aaaa [1] => wwww [2] => eeee [4] => tttt [5] => WWWW )
連想配列の場合
qiita.php
$array = array(
    array('takuya', 'jun', 'yoshiko', 'miki'),
    array('kazuki', 'aya', 'yoshiko', 'miki'),
    array('takuya', 'jun', 'yoshiko', 'miki'),
);

$array = array_unique($array, SORT_REGULAR);
//Array ( [0] => Array ( [0] => takuya [1] => jun [2] => yoshiko [3] => miki ) [1] => Array ( [0] => kazuki [1] => aya [2] => yoshiko [3] => miki ) )

第2引数にSORT_REGULARを指定しました。

配列内に要素が何種類あるか調べる。

qiita.php
$array = ["aaaa","wwww","eeee","aaaa","tttt","WWWW","eeee"];
$count = count(array_unique($array));
//5
qiita.php
$array = array(
    array('takuya', 'jun', 'yoshiko', 'miki'),
    array('kazuki', 'aya', 'yoshiko', 'miki'),
    array('takuya', 'jun', 'yoshiko', 'miki'),
);

$count = count(array_unique($array, SORT_REGULAR));
//2

//要素が全て同じ
$array = array(
    array('takuya', 'jun', 'yoshiko', 'miki'),
    array('takuya', 'jun', 'yoshiko', 'miki'),
    array('takuya', 'jun', 'yoshiko', 'miki'),
);

$count = count(array_unique($array, SORT_REGULAR));
//1
toadstomochi
PHPの勉強を始めた初心者です。新しく覚えた知識などを、知らなかった頃の自分に向けて書くイメージで記事を更新してます。内容は初歩的なことばかりですが頑張って書きます。だんだんとレベルアップして行きたいです!ご指摘あればお願いいたします!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした