2016-05-15 8 views
2

わかりにくいかもしれません..ご疑問がありましたらお聞かせください。 2配列ループからPHP 2つの多次元配列から特定のレコードを取得できません

$firstArray=array(array('startDate'=>'5-05-2016',endDate=>'10-05-2016') 
array('startDate'=>'5-06-2016',endDate=>'10-0-2016')); //multiple records 
$secondArray=array(array('date'=>'07-05-2016',array('date'=>'07-06-2016'))); //multiple recods 

// this is what I tried 
if($firstArray){ 
foreach($firstArrat $intArr){ 


    if($secondArray){ 
     foreach($secondArray as $sArr){ 
      if (($intArr['startDate'] < $sArr['date']) && ($intArr['endDate'] > $sArr['date'])){ 
       echo $sArr['date']; 
      } 
      else{ 
       echo $sArr['date']; 
      } 
      } 
      } 
     } 

がある私は、日付は私がout.records複製助けない最初の配列のループで二番目の配列をdate.looping間と最初の配列の開始日と終了の場合は二番目の配列から特定のデータを取得する必要があります。 1つのレコードのみが一致する必要があります。助けてください 。海の真ん中にこだわった。

+0

何か試しましたか? –

+0

はい。私は最初の配列でscond配列ループをループして取得しようとしましたが、それはhelp.recordsを複製しませんでした。そのレコードと一致するレコードが別の配列に移動しなければならない場合一致しない場合、別の配列に移動する必要があります。 – Shahmee

+0

明確にするには、 '$ firstArray = array(array( 'startDate' => '5-05-2016'、endDate => '10-05-2016')、...))'? –

答えて

2

これを行うだけで、最初の配列の開始と終了の間にあるかどうかを調べることができるsecondArrayへのループを使用します。 $firstArrayには、これらの2つのエントリが必要です。

オンラインで確認してください。Online test

タイムスタンプに変更する場合は、strtotimeを使用してください。

$firstArray = array(
       array('startDate' => '05-05-2016', 'endDate' => '10-05-2016'), 
       array('startDate' => '05-06-2016', 'endDate' => '10-07-2016'), 
       array('startDate' => '05-08-2016', 'endDate' => '10-11-2016') 
      ); 

$secondArray = array(array('date' => '07-05-2016'), array('date' => '12-07-2016'), array('date' => '12-11-2016')); 

function find_match($index){ 
    global $firstArray, $secondArray; 
    foreach($secondArray as $date){ 
     if(strtotime($date['date']) > strtotime($firstArray[$index]['startDate']) && strtotime($date['date']) < strtotime($firstArray[$index]['endDate'])) 
      return $date['date']; 
    } 
    return null; 
} 

foreach($firstArray as $key => $st_dates){ 
    $date = find_match($key); 
    echo ($date != null) ? $date : "No match Found."; 
    echo '<br/>'; 
} 

結果:私は機能上から自分のコードをデバッグするように固定

07-05-2016 
No match Found. 
No match Found. 
+0

私はこのようにして既に重複しているレコードを試しました。そのレコードと一致するレコードが別の配列に移動しなければならない場合一致しない場合、別の配列に移動する必要があります。 両方とも複数のレコードを持っていることを理解してください。 – Shahmee

+0

彼は全部をプログラムしたいですか? – iGNEOS

+0

私はundestandをしなかった。一つのチップでも助けてくれるでしょう。inArrayを使う方法はありません。私はそれが難しいと思っています。 – Shahmee

1

いくつかのこと。関数はそのようにする必要があります

function find_match($index){ 
$data=null; 
global $firstArray, $secondArray; 
foreach($secondArray as $date){ 
    if(strtotime($date['date']) > strtotime($firstArray[$index]['startDate']) && strtotime($date['date']) < strtotime($firstArray[$index] ['endDate'])) 
     $data=$date['date']; 
} 
if($data !=null){ 
    return $data; 
}else{ 
    return null 
} 
} 
関連する問題