Shahmee Shahmee - 2 years ago 80
PHP Question

Unable to get specific record from PHP two multi dimensional array by loop

It might be difficult to understand .. please ask if u have any doubt.
there are two array

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
foreach($firstArrat $intArr){

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

I need to get the specific data from second array if date is between and first array start date and end date.looping second array in first array loop not helping me out.records duplicating.only one record should be match. Please help . stuck in middle of ocean.

Answer Source

Just do this, using a loop to secondArray you can check is it between the first array start and end or not. $firstArray must have these two entries.

Check this online Online test.

Convert each time to time stamp using 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/>';


No match Found.
No match Found.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download