PHP search array

Associate
Joined
30 Dec 2005
Posts
415
Ok here's a puzzle which i'm not sure how to go about....

I've got an array which looks like this:
Code:
[0] => 1140220800
[1] => 1142640000
[2] => 1140120800
[3] => 1142340000
They are all timestamps.

Basically, if I specify a value, for example 1142540000, I want it to find all values within 7 days of that value, whether in the past or the future, and then ultimately, return the closest.

How would I go about this/has anyone done this before?

Cheers,
Rich
 
How big is the array?

If it's relatively small, can you not just do:

Code:
$value = 1142540000;

foreach($array as $k => $v) {
    if($v > ($value - 7 * 86400) || $v < ($value + 7 * 86400)) {
        $values[] = $v;
    }
}

var_dump($values);
 
Sorry I'm a bit stuck with that code...

If the array is in a session ( $_SESSION["startdate"] ), how do I implement this? I'm not sure what the statement foreach($array as $k => $v) is doing!

Code:
$value = 1142540000;

foreach($array as $k => $v) {
    if($v > ($value - 7 * 86400) || $v < ($value + 7 * 86400)) {
        $values[] = $v;
    }
}

var_dump($values);

Sorry i've lost it today!
 
Last edited:
Ok cheers.
From what I can see of that script, it returns any values which are within 7 days of the original value, whether in the past of the future.. how do you return the closest?
 
Code:
$value = 1142540000;

foreach($array as $k => $v) {
    if($v > ($value - 7 * 86400) && $v < ($value + 7 * 86400)) {
        if (abs($value - $v) < abs($value - $closest)) {
            $closest = $v;
        }
        $values[] = $v;
    }
}

var_dump($closest);
var_dump($values);
That should do the trick :)
 
Last edited:
It returns this:

Code:
string(10) "1142640000"
array(4) {
  [0]=>
  string(10) "1140220800"
  [1]=>
  string(10) "1142640000"
  [2]=>
  string(10) "3140120800"
  [3]=>
  string(10) "1140120800"
}
Cheers :D
 
Ok I don't think it works actually...
In the array are the following values:
Code:
Array ( [0] => 1141430400 [1] => 1141603200 [2] => 1141344000 )

The figure i'm comparing for is 1141641263.
The script returns the key 2. Am I right in thinking it should return 1?
PHP:
$value = time();
foreach($_SESSION["startdate"] as $k => $v) {
   if($v > ($value - 7 * 86400) || $v < ($value + 7 * 86400)) {
      if (abs($value - $v) < abs($value - $closest)) {
         $closest = $k;
      }        
   }
}

See edited post... should work now
Surely it would be || instead of &&, as it is looking for a value which is within the limit in either direction, not in both directions?
 
Ah it's alright, I thought about it and i've written my own method which appears to work :)

Code:
$i = 0;
$lower = 0;
$higher = 9999999999;
oreach ($_SESSION["startdate"] as $a) {
    if ($a<$value) { //Determined that it is lower than the target
        if ($a>$lower) {
            $lower = $a;
            $lowerkey = $i;
        }    
    }
    if ($a>$value) { //Determined that it is higher than the target
        if ($a<$higher) {
            $higher = $a;
            $higherkey = $i;
        }    
    }
$i++;     
}
echo "<br />Target: ".$value;
echo "<br />Lower: ".$lowerkey." - ".$lower." - ".$_SESSION["hikes"][$lowerkey];
echo "<br />Higher: ".$higherkey." - ".$higher." - ".$_SESSION["hikes"][$higherkey];
$lowerdiff = $value - $lower;
$higherdiff = $higher - $value;
if ($lowerdiff<=$higherdiff)
    echo "<br />".$_SESSION["hikes"][$lowerkey]." is the closest";
if ($lowerdiff>$higherdiff)
    echo "<br />".$_SESSION["hikes"][$lowerkey]." is the closest";

This is the output:

Code:
Target: 1141664530
Lower: 0 - 1141662181 - 49
Higher: 1 - 1141786045 - 76
49 is the closest
Lower diff: 2349
Higher diff: 121515
 
Last edited:
Back
Top Bottom