Jump to content

[SOLVED] in_array problem


JJohnsenDK

Recommended Posts

Hey

 

I have this function which checks if an number is in a array. What i am using this for is to check if a periode of days is booked in a calendar.

The function works fine if i just manually write the numbers into the array. But i want to use the variable i create with the numbers. The wierd

thing is that this works if i make a foreach on the array. The in_array() function just wont work when i declare the array with an variable.

 

Here is the code:

 


function checkBusyDate($day, $month, $year){
$date = $day."/".$month."/".$year;

$query = mysql_query("SELECT toa FROM calendar WHERE froma = '$date'");
$data = mysql_fetch_array($query);

$from 	= $day;
$to 	= substr($data['toa'], 0, 2);

while($from < $to){
	$numForArray .= $from.", ";
	$from++;
}

$numForArray .= $to;
$array = array($numForArray);

if(in_array(25, $array, true)){
	echo "Yep its there.";
}else{
	echo "Nopez, sry";
}
}

checkBusyDate(24, 12, 2007);

 

Does'nt in_array() support what im trying to do or whats wrong?

Link to comment
https://forums.phpfreaks.com/topic/82676-solved-in_array-problem/
Share on other sites

$array = array($numForArray) would give the array only one value. Try this:

 

<?php

function checkBusyDate($day, $month, $year){
$date = $day."/".$month."/".$year;

$query = mysql_query("SELECT toa FROM calendar WHERE froma = '$date'");
$data = mysql_fetch_array($query);

$from 	= $day;
$to 	= substr($data['toa'], 0, 2);

$array = array();

while($from < $to){
	$array[] = $from;
	$from++;
}

$array[] = $to;

if(in_array(25, $array)){
	echo "Yep its there.";
}else{
	echo "Nopez, sry";
}
}

checkBusyDate(24, 12, 2007);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.