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 5 years have passed since last update.

多次元配列の値比較

Posted at

#多次元多次元配の値の比較について
下記コードは中身がわかるように配列A、Bを作成した後、
配列A,Bに格納されている時間を比較して、
合致すれば結果値格納用配列に。
合致しなければ格納しない。
という単純そうなコードです。

やる前は簡単だろと思っておりましたが、
多次元配列同士の比較がうまくされず、とても時間をかけてしまいました。
以下が私が最終的に作成したコードです。
※もっと効率的なやり方あれば教えていただけるとありがたいです・・・

#コード

<?php
//多次元配列Aの作成
$list = array(
  '10' => array(
      '1' => '10:00', 
      '2' => '10:15', 
      '3' => '10:30', 
      '4' => '10:45'
  ),
  '11' => array(
      '1' => '11:00', 
      '2' => '11:15', 
      '3' => '11:30', 
      '4' => '11:45'
  ),
  '12' => array(
      '1' => '12:00', 
      '2' => '12:15', 
      '3' => '12:30', 
      '4' => '12:45'
  ),
  '13' => array(
      '1' => '13:00', 
      '2' => '13:15', 
      '3' => '13:30', 
      '4' => '13:45'
  ),
  '14' => array(
      '1' => '14:00', 
      '2' => '14:15', 
      '3' => '14:30', 
      '4' => '14:45'
  ),
  '16' => array(
      '1' => '16:00', 
      '2' => '16:15', 
      '3' => '16:30', 
      '4' => '16:45'
  )
);

//多次元配列Bの作成
$list2 = array(
  '10' => array(
      '1' => '10:01', 
      '2' => '10:15', 
      '3' => '10:31', 
      '4' => '10:45'
  ),
  '11' => array(
      '1' => '11:01', 
      '2' => '11:15', 
      '3' => '11:30', 
      '4' => '11:45'
  ),
  '12' => array(
      '1' => '12:01', 
      '2' => '12:15', 
      '3' => '12:30', 
      '4' => '12:45'
  ),
  '13' => array(
      '1' => '13:01', 
      '2' => '13:15', 
      '3' => '13:30', 
      '4' => '13:45'
  ),
  '14' => array(
      '1' => '14:01', 
      '2' => '14:15', 
      '3' => '14:30', 
      '4' => '14:45'
  ),
  '15' => array(
      '1' => '16:01', 
      '2' => '16:15', 
      '3' => '16:30', 
      '4' => '16:45'
  )
);

//多次元配列A
//多次元を比較しやすいように変換しております。
foreach ($list as $key1 => $val1) {
    foreach ($val1 as $key2 => $val2) {
        $text1[] = $val2;
    }
}
//多次元配列B
//多次元を比較しやすいように変換しております。
foreach ($list2 as $key1 => $val1) {
    foreach ($val1 as $key2 => $val2) {
        $text2[] = $val2;

    }
}
//多次元配列AとBの照合
//合致すれば結果($answer)に格納
foreach ($text1 as $key => $val) {
        if(in_array($val , $text2)){
            $answer[] = $val;
        }
}
print_r($answer );

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