Bloodhound
Bloodhound

Reputation: 2966

Compare an array based on previous value of the array

I have an array that looks like this:

Array ( 
    [0] => Array (  
        [START] => COI-COK 
        [RETURN] => CAI - DEL 
    ) 
    [1] => Array ( 
        [START] => COK - AMM 
        [RETURN] => CAI - DEL 
    ) 
)

I want to check if both 'start' and 'end' values of previous and current array are same or not. If not then, print some value. How can I do it?

This is my attempt:

foreach($data as $datas)
{     
    $old_start  = $datas['START'];
    $old_return = $datas['RETURN'];

    ...

    if( ($old_start == $datas['START']) && ($old_return == $datas['RETURN']))
    {

    }
    else
    {

    }
}

But it didn't work because all the time old_start value will be equal to $datas['START'].

print_r($data) shows this output:

Array ( 
    [0] => Array ( 
        [Sl] => 2 
        [TRAVELDAY] => 2015-11-11 
        [RETURNDAY] => 2015-11-27 
        [START] => COI-COK 
        [RETURN] => CAI - DEL 
    ) 
    [1] => Array ( 
        [Sl] => 1 
        [TRAVELDAY] => 2015-11-11 
        [RETURNDAY] => 2015-11-27 
        [START] => COK - AMM 
        [RETURN] => CAI - DEL 
    ) 
)

Upvotes: 0

Views: 54

Answers (4)

syck
syck

Reputation: 3029

You have to put the assignment after the comparison, not before:

$old_start = '';
$old_return = '';
foreach($data as $datas)
{     
     //....
     if($old_start=='' || $old_start == $datas['START'] && $old_return == $datas['RETURN'])
     {
         //....
     }
     else
     {
         //code to be executed
     }

     $old_start = $datas['START'];
     $old_return = $datas['RETURN'];
}

Upvotes: 2

Amarnasan
Amarnasan

Reputation: 15529

foreach($data as $sample) {
    if (!isset($temp)) {
        $temp = $sample;
    } else {
        if ($temp['START']==$sample['START'] && $temp['RETURN']==$sample['RETURN']) {
            ; //WHATEVER EQUAL
        } else {
            ; //WHATEVER NOT EQUAL
        }
        $temp = $sample;
    }
}

Upvotes: 1

Vivek Tankaria
Vivek Tankaria

Reputation: 1311

$array = Array ( 0 => Array ( "START" => "COI-COK", "RETURN" => "CAI - DEL"), 1 => Array ( "START" => "COK - AMM","RETURN" => "CAI - DEL" ),2=> Array ( "START" => "COK - AMM","RETURN" => "CAI - DEL" ) );

$old_start = "";
$old_return = "";
foreach($array as $ak=>$av){
    if(empty($old_start)){
        $old_start = $av['START'];
        $old_return = $av['RETURN'];
    }else{
        if($old_start == $av['START'] && $old_return == $av['RETURN']){
            echo "SAME\n";
        }else{
            echo "Varies\n";
        }

        $old_start = $av['START'];
        $old_return = $av['RETURN'];
    }
}

//Output 
Varies //key 1 will not match with key 0 value
Same   // key 2 will not match with key 1 value

Hope this clears your logic, add as many array value you want , I modified your array from 2 to 3 to show the difference. This will compare n with n+1 and show the result.

Upvotes: 0

Amit Rajput
Amit Rajput

Reputation: 2059

Try like this..

$old_start = "";
$old_return = "";

foreach($data as $datas)
{     

    if( ($old_start == $datas['START']) && ($old_return == $datas['RETURN']))
    {
        //true code to be executed
    }
    else
    {
        //false code to be executed
    }

    $old_start = $datas['START'];
    $old_return = $datas['RETURN'];
}

Upvotes: 1

Related Questions