0

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 
    ) 
)
6
  • $data's output is the array i have given Commented Nov 6, 2015 at 10:32
  • Do you have always only two arrays to compare? Commented Nov 6, 2015 at 10:33
  • its not two array its only one array with many values Commented Nov 6, 2015 at 10:35
  • thanks brother @AmalMurali Commented Nov 6, 2015 at 10:50
  • @AjithN: No problem; in future, please do make sure to format your questions neatly. Tip: you can add header('Content-Type: text/plain;charset=utf8'); at the very top of your script to see the output produced exactly as it is. Commented Nov 6, 2015 at 10:52

4 Answers 4

2

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'];
}
Sign up to request clarification or add additional context in comments.

7 Comments

$old_start won't be initialized inside the loop - this will just throw an Undefined Variable error.
@AmalMurali If you can see the edit history, you will see that I edited it in before you posted your comment. Thanks for the constructive downvote.
@AjithN The code in the else path? You only have to check whether for example $old_start==''.
@syck: Apparently yes, I've rescinded my downvote and made it an upvote.
@AjithN edited the code so it won't trigger on first loop.
|
1

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'];
}

1 Comment

This will go in false for 0 key array. Which is not to be tested.
1
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;
    }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.