0
0

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 1 year has passed since last update.

配列内の値が同一だった場合の判定プログラム

Posted at

背景

よくあるゲームのパーティ構成で同じキャラクターを選べない場合があると思います。
その際に考えることになったためです。

記事を記載した目的

ただの記録です。

やりたいこと

パーティという配列内のキャラクターが同一だった場合の判定プログラム

実際のコード

<?php


$party = array(  
  0 => "1",
  1 => "1",
  2 => "1",
  3 => "1",
  4 => "1",
  5 => "1",
  );
  
  $result =array_unique($party);
  $duplicatedCharacters = count($party) - count($result);
  
  if ($duplicatedCharacters < 1) {
        echo "重複していません";
    }else{
        echo"重複しています。";
    }

?>

主な関数

配列内の値が同一だった場合は、ture 同一でない場合は、falseの関数がないらしい(記事作成時点)
あったら教えてください。有識者さん

array_unique — 配列から重複した値を削除する
重複していた場合は、

$input =  = array(  
  0 => "1",
  1 => "1",
  2 => "1",
  3 => "2",
  4 => "1",
  5 => "1",
  );
Array
(
    [0] => 1
    [3] => 2
)


このように返ってくる。

$inputの配列の数から$outputの配列の数を引いてその差が1未満だった場合は、重複していません。
そうではない場合は、重複しています。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?