Newbie question re: complex array search and output

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
Barkord
Forum Newbie
Posts: 2
Joined: Thu Jul 28, 2005 12:59 pm

Newbie question re: complex array search and output

Post by Barkord »

Ahh yes, nuthin' like another newbie question.

I am constructing an events calendar and I am having trouble posting array elements.
Depending on what day of the month it is, I need a function to perform a conditional "if" statement search through an array and post the array elements from within the key EventID. So my problem is that the function I use works fine, but my echo call (see bottom of this post) only displays the LAST element in the array that conforms to the "if" statement. What can I do to properly echo the array elements that conform to the "if" statment and that are within the scope of the EventID key?

Here is my array constructor:
while ($row = mysql_fetch_assoc($q_getEvents_result)) {
$EventStruct[$EventID=($row['EventID'])] = array(
"EventName" => ($EventName=($row['EventName'])),
"ESTime" => ($StartTime=($row['ESTime'])),
"EETime" => ($EndTime=($row['EETime'])),
"ESDay" => ($ESDay=($row['ESDay'])),
"EEDay" => ($EEDay=($row['EEDay'])),
"ESMonth" => ($ESMonth=($row['ESMonth'])),
"EEMonth" => ($EEMonth=($row['EEMonth'])),
"ESYear" => ($ESYear=($row['ESYear'])),
"EEYear" => ($EEYear=($row['EEYear'])),
"TimeZone" => ($TimeZone=($row['TimeZone'])),
"EventTypeColor" => ($EventTypeColor=($row['EventTypeColor'])));
}

Here is the function (from php.net):
function array_search_recursive($needle, $haystack, $key_lookin="") {
$path = NULL;
if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) {
$path[] = $key_lookin;
} else {
foreach($haystack as $key => $val) {
if (is_scalar($val) && $val === $needle && empty($key_lookin)) {
$path[] = $key;
break;
}
elseif (is_array($val) && $path = array_search_recursive($needle, $val, $key_lookin)) {
array_unshift($path, $key);
break;
}
}
}

Here is how I am calling the function:

//The $date_no1 is the variable used to output the days in my calendar

if ((array_search_recursive($date_no1, $EventStruct, $key_lookin="ESDay"))) {
echo "<br>".($EventStruct[$EventID]["EventName"])."<br>";
}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Newbie question re: complex array search and output

Post by josh »

First off use PHP tags
viewtopic.php?t=21171
Barkord wrote:

Code: Select all

while ($row = mysql_fetch_assoc($q_getEvents_result)) {
	$EventStruct[$EventID=($row['EventID'])] = array(
		"EventName" => ($EventName=($row['EventName'])),
		"ESTime" => ($StartTime=($row['ESTime'])),
		"EETime" => ($EndTime=($row['EETime'])),
		"ESDay" => ($ESDay=($row['ESDay'])),
		"EEDay" => ($EEDay=($row['EEDay'])),
		"ESMonth" => ($ESMonth=($row['ESMonth'])),
		"EEMonth" => ($EEMonth=($row['EEMonth'])),
		"ESYear" => ($ESYear=($row['ESYear'])),
		"EEYear" => ($EEYear=($row['EEYear'])),
		"TimeZone" => ($TimeZone=($row['TimeZone'])),
		"EventTypeColor" => ($EventTypeColor=($row['EventTypeColor']))); 
}
Here is the function (from php.net):

Code: Select all

function array_search_recursive($needle, $haystack, $key_lookin="") {
$path = NULL;
   if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) {
   $path[] = $key_lookin;
   } else {
       foreach($haystack as $key => $val) {
           if (is_scalar($val) && $val === $needle && empty($key_lookin)) {
           $path[] = $key;
           break;
           }       
           elseif (is_array($val) && $path = array_search_recursive($needle, $val, $key_lookin)) {
           array_unshift($path, $key);
           break;
           }
       }
   }

Here is how I am calling the function:

Code: Select all

//The $date_no1 is the variable used to output the days in my calendar  

	if ((array_search_recursive($date_no1, $EventStruct, $key_lookin="ESDay"))) {	
		echo "<br>".($EventStruct[$EventID]["EventName"])."<br>";
	}
Barkord
Forum Newbie
Posts: 2
Joined: Thu Jul 28, 2005 12:59 pm

Answered my own question

Post by Barkord »

I was stumped for a while which is why I finally posted this request. How was I to know that I was just a cut'n'paste away from accomplishing what I need. I simply put my echo call within the funtion. Viola... success. Now to clean up and optimize.

function display_event($needle, $haystack, $key_lookin="") {

$path = NULL;

if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) {
$path[] = $haystack[$key_lookin];

echo "<table><tr><td bgcolor='".$haystack["EventTypeColor"]."'><font style='font-size: 10pt;'>".$haystack["EventName"]."</font></td></tr></table>";

} else {

foreach($haystack as $key => $val) {
if (is_scalar($val) && $val === $needle && empty($key_lookin)) {
$path[] = $key;
break;
}

elseif (is_array($val) && $path = display_event($needle, $val, $key_lookin)) {
array_unshift($path, $key);
break;
}
}
}

return $path;
}
Post Reply