array_search() on multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

array_search() on multidimensional array

Post by Burrito »

I have a multidimensional array (thanks to Tim's concept) that I need to search for values on and can't quite get my brain into the correct gear to figure this out.

Code: Select all

<?
$getstuff = mysql_query("select * from 21_dates where date = '".$_GET['day']."' order by starttime")
	or die(mysql_error());
$events = array();
while($gtstuff = mysql_fetch_assoc($getstuff)){
       //create events array...burrito
	$events[] = array('title'=>$gtstuff['eventtitle'],'start'=>$gtstuff['starttime'],'end'=>$gtstuff['endtime']);
}
?>

I then loop over every hour during the day and within that hour loop over every 15 minute interval. I need to use the values within my second for loop for the start time of that for loop and check it against the value for the start key in my second array. I thought about using array_search() to accomplish this, but can't think the logic through. Can someone either help me with that, or provide me with an alternative solution to extracting all of the data from the parent array's key if the child array's start key matches my increment value?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

after a second thought (brain is really dead now :p) i would more choose for a solution like the one below:

Code: Select all

$active = array();

$sql = "SELECT * FROM table WHERE day=$day ORDER BY starttime ASC";
$rs = mysql_query($sql);
$row = mysql_fetch_assoc($rs);
if (is_null($row))
{
   $row['starttime'] = 9999999999;
}

$time = strtotime("$year-$month-$day 00:00:00");
while ($time < strtotime("$year-$month-$day 00:00:00 +1 day"))
{
  // fetch events that start in this quarter...
  while ($time > $row['starttime'])
  {
    $active[] = $row;
    $row = mysql_fetch_assoc($rs);
    if (is_null($row))
    {
    $row['starttime'] = 9999999999;
    }
  }
  
  // now display them, only keep those that have not ended
  $newactive = array();
  foreach($active as $row)
  {
    if ($row['stoptime'] <= $time)
    {
      // event stops here
    }
    else
    {
      //event still going on
      $newactive[] = $row;
    }
  }
  $active = $newactive;

  $time += 15 * 60;
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

cheers Tim, I might use that for a solution: I did however figure out a way to do the array_search() on a multidimensional array, it's sort of a hack-job, but it works :P

Code: Select all

<?
$bob = array();
$bob[] = array('one'=>'oner','two'=>'twoz');
$bob[] = array('one'=>'threez','two'=>'fourz');
$count = 0;
$newarr = array();
foreach($bob as $thisbob => $thatbob){
	if($key = array_search("twoz",$thatbob)){
		$newarr[] = $count;
	}
	$count++;
}
foreach($newarr as $newarrval){
	echo $bob[$newarrval]['one']." ".$bob[$newarrval]['two'];
}
?>
obviously that's just test stuff and wouldn't apply to the picture I painted, but I needed to see if I could make it work and indeed it does...

thx again Tim, I'll look more closely at your suggestion next week, have a wonderful weekend.

Burr
Post Reply