comparing numeric array values side by side

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
Hope people can help me, I am trying to build some validation into my script that updates statistics each week.

I have an array that has $ThisWeeksStats in it and I want to ensure it is not equal to $LastWeeksStats and to ensure that $ThisWeeksStats is numeric

I realise that statistically it is possible to have the same stats but it is highly unlikely to happen :D

I currently have:

PHP:
if ($ThisWeeksStats != $LastWeeksStats && is_numeric($ThisWeeksStats)) {
    print '$ThisWeeksStats';
}
else {
    print 'either the numbers are equal to each other or there isnt numerics in this weeks results';
}

$ThisWeeksStats consists of:
Code:
key: 0 , value: 976 
key: 1 , value: 830 
key: 2 , value: 444 
key: 3 , value: 898 
key: 4 , value: 2325
key: 5 , value: 1599

$LastWeeksStats consists of:
Code:
key: 0 , value: 5103
key: 1 , value: 1367 
key: 2 , value: 790 
key: 3 , value: 665 
key: 4 , value: 88 
key: 5 , value: 5432

The if statement doesn't seem to work so I am assuming it isn't comparing the arrays properly?
 
Assuming we're dealing with PHP here.

$array1 == $array2 will evaluate to true if both arrays contain the same keys and values.

$array1 === $array2
will evaluate to true if both arrays contain the same keys and values and the elements are of the same type and in the same order.

This means the first part of your if statement is fine. The is_numeric($ThisWeeksStats) will always return false as $ThisWeeksStats is not a number, it's an array. You will need to evaluate is_numeric on each element of the array to get the correct result. You can use a for/for each loop to do this, you may also want to look at some of the array functions such as array_map() and array_walk().
 
Why not put it in an object with a timestamp then you'll know what week it's for?

being different from the previous week is a weak way of determining if the stats are new.
 
Why not put it in an object with a timestamp then you'll know what week it's for?

being different from the previous week is a weak way of determining if the stats are new.

Yeh I see what you're saying but there is no guarantee that someone has updated the new stats in the remote xml so i need to check the numbers are not the same between the two arrays.


hhhmmm but how would I validate if the numbers are different if I only used timestamps? If I am following I could end up with duplicate data if I only looked at the timestamp? Thats if nobody updates the remote xml when my script is run.

I'm scraping stats off a remote xml.
 
Last edited:
Yeh I see what you're saying but there is no guarantee that someone has updated the new stats in the remote xml so i need to check the numbers are not the same between the two arrays.


hhhmmm but how would I validate if the numbers are different if I only used timestamps? If I am following I could end up with duplicate data if I only looked at the timestamp? Thats if nobody updates the remote xml when my script is run.

I'm scraping stats off a remote xml.

cache md5 of the array? then check if the old md5 is different to the new one, if it is then the stats have changed.
 
Back
Top Bottom