kevinabraham
kevinabraham

Reputation: 1437

How to compare last 2 elements of an array?

I have an array:

array:8 [▼
  0 => array:1 [▼
    "data" => "789"
  ]
  1 => array:1 [▼
    "data" => "800"
  ]
  2 => array:1 [▼
    "data" => "789"
  ]
  3 => array:1 [▼
    "data" => "787"
  ]
  4 => array:1 [▼
    "data" => "787"
  ]
  5 => array:1 [▼
    "data" => "787"
  ]
  6 => array:1 [▼
    "data" => "787"
  ]
  7 => array:1 [▼
    "data" => "787"
  ]
]

I need to take out the last 2 elements of the array and compare them. I tried using $getLast2 = array_slice($chart_data, -2, 2, true); to get the last 2.

array:2 [▼
  6 => array:1 [▼
    "data" => "787"
  ]
  7 => array:1 [▼
    "data" => "787"
  ]
]

Which then splits it. But Im not sure how to compare these 2 elements within this new array. As the last 2 elements which are now 6 and 7 could change as more data is added. I basically need to tell if the first element is great than, less than or equal to the second element.

Upvotes: 1

Views: 695

Answers (5)

Poiz
Poiz

Reputation: 7617

If it suits you, you can just use the array_pop to do the comparison in this manner:

    <?php
        $array = [
            ["data" => "789"],
            ["data" => "800"],
            ["data" => "789"],
            ["data" => "787"],
            ["data" => "787"],
            ["data" => "789"],
            ["data" => "787"],
            ["data" => "787"],
        ];

    // MAKE A COPY OF THE ORIGINAL ARRAY:
    $arrayCopy      = $array;

    // POP THE LAST ELEMENT OFF THE $arrayCopy AND SAVE IT AS $lastElem:
    $lastElem       = array_pop($arrayCopy);

    // POP THE LAST ELEMENT OFF THE $arrayCopy AGAIN AND SAVE IT AS $beforeLastElem:
    $beforeLastElem = array_pop($arrayCopy);

    // NOW YOU CAN COMPARE THE LAST ELEMENT AND THE ONE BEFORE IT
    if($lastElem == $beforeLastElem){
        echo "The last 2 Elements of \$array are the same";
    }else{
        echo "The last 2 Elements of \$array are not identical.";
    }

Upvotes: 0

Webeng
Webeng

Reputation: 7113

If the array you obtained from your previous code is called $splitArray, then you could do the following:

list($array1, $array2) = $splitArray;
if ($array1['data']>$array2['data'])
{
  echo "1st is greater than 2nd<br>";
}
else
{
  echo "1st is not greater than 2nd<br>";
}

Upvotes: 0

devnull
devnull

Reputation: 1928

You have output this array

array:2 [▼
  6 => array:1 [▼
    "data" => "787"
  ]
  7 => array:1 [▼
    "data" => "787"
  ]
]

Re-index them to using PHP array_values() function

$outputedArray = array_values($outputedArray)
 if($outputedArray[0]['data'] > $outputedArray[1]['data'])
     echo "0 index is greater";

Upvotes: 0

Pratik Soni
Pratik Soni

Reputation: 2588

Pass last variable as false;

preserve_keys Note that array_slice() will reorder and reset the numeric array indices by default. You can change this behaviour by setting preserve_keys to TRUE.

array_slice($chart_data, -2, 2, false);

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163808

You can use built in end() function and then prev():

if (end($chart_data) == prev($chart_data)) {
    echo 'Two last elements of an array are equal!';
}

Upvotes: 1

Related Questions