kracklt
asked on
Search Multidimensional array
Hi Experts:
Is there a way to search a multidimensional array in a specific dimension? Here is an example:
$rented =
(
[0] => Array
(
[id] => 182
[start] => 13-03-2006 00:00:00
[length] => 48.00
)
[1] => Array
(
[id] => 194
[start] => 15-03-2006 08:00:00
[length] => 4.00
)
[2] => Array
(
[id => 194
[start] => 15-03-2006 12:00:00
[length] => 5.00
)
Now I need to create a table which displays rented time for each id (each id represents a car) as red and free time as green. I am trying to do it like this:
for ($d=0; $d<21;$d++) // go through 21 days
{
for ($h=0; $h<24;$h++) // every day of the hour
{
$blocktime = date("d-m-Y H:00:00", mktime($h,0,0,date($datum_ split[1]), date($datu m_split[2] )-$start_o ffset+$d,d ate($datum _split[0]) ));
if (array_key_exists(($rented [$car_id][ 1], $blocktime)) // check in array if blocktime exists for a specific car.
{
$block[$car_id][$blocktime ] = "taken";
} else {
// no change
$block[$car_id][$blocktime ] = "free";
}
The code above produces the error: Wrong datatype for second argument ( besides the key does not exist error which I think I can overcome).
Well, this is how far I am getting. I think I can do it this way but I would need to find a way to search in a certain dimension of the array (ideally in a timesaving way). If anyone has a completely different approach I am also open for that, as I am not sure if this is really ideal coding....
Thanks,
Thomas :-)
Is there a way to search a multidimensional array in a specific dimension? Here is an example:
$rented =
(
[0] => Array
(
[id] => 182
[start] => 13-03-2006 00:00:00
[length] => 48.00
)
[1] => Array
(
[id] => 194
[start] => 15-03-2006 08:00:00
[length] => 4.00
)
[2] => Array
(
[id => 194
[start] => 15-03-2006 12:00:00
[length] => 5.00
)
Now I need to create a table which displays rented time for each id (each id represents a car) as red and free time as green. I am trying to do it like this:
for ($d=0; $d<21;$d++) // go through 21 days
{
for ($h=0; $h<24;$h++) // every day of the hour
{
$blocktime = date("d-m-Y H:00:00", mktime($h,0,0,date($datum_
if (array_key_exists(($rented
{
$block[$car_id][$blocktime
} else {
// no change
$block[$car_id][$blocktime
}
The code above produces the error: Wrong datatype for second argument ( besides the key does not exist error which I think I can overcome).
Well, this is how far I am getting. I think I can do it this way but I would need to find a way to search in a certain dimension of the array (ideally in a timesaving way). If anyone has a completely different approach I am also open for that, as I am not sure if this is really ideal coding....
Thanks,
Thomas :-)
ASKER
Dear siliconbrit:
Thank you for your quick answer. This would work but the problem is that there is more than one car respectively more than one rental time....
$rented[$car_id]['start'] == $blocktime does not deliver anything because what is missing is the outer key...
$rented[x][$car_id]['start '] == $blocktime would work, but I don't know which x I would need. Thats when I was starting to experiemnt with in_array() or array_key_exists() (<-- which BTW you are probably also right about that I won't get sensible information out of that.)
Any ideas?
Thomas
Thank you for your quick answer. This would work but the problem is that there is more than one car respectively more than one rental time....
$rented[$car_id]['start'] == $blocktime does not deliver anything because what is missing is the outer key...
$rented[x][$car_id]['start
Any ideas?
Thomas
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Siliconbrit:
It worked. I was sort of hoping to get around having to test each array element to increase speed. However, this seems to be the easiest method and it works perfectly.... Her is the final code:
for ($c=0; $c <count($s);$c++)
{
$helper_id = $s[$c][0]; // this is the car_id
for ($d=0; $d<21;$d++) // check 21 days
{
for ($h=0; $h<24;$h++) // every hour
{
$blocktime = date("d-m-Y H:00:00", mktime($h,0,0,date($datum_ split[1]), date($datu m_split[2] )-$start_o ffset+$d,d ate($datum _split[0]) ));
for ($m = 0; $m<count($rented2);$m++) // loop through every recorded rental
{
if ($rented2[$m][1] == $blocktime and $rented2[$m][0] == $helper_id and !isset($jump))
{
$block[$helper_id][$blockt ime] = "taken"; // record start time as rented
$jump = $rented2[$m][2]; // set amount of hours rented
break;
}
} // end all rentals
if (!isset($jump))
{
$block[$helper_id][$blockt ime] = "free";
} else {
$block[$helper_id][$blockt ime] = "taken";
$jump = $jump - 1;
if ($jump == 0) // length has been fullfilled -> next hour is free
{
unset ($jump);
}
}
} // end hours
} // End days
unset($jump); // unset jump so that the taken hours don't overlap to next car.
} // End cars
Hope this helps somebody. I will have a look at the array_walk functionality. Although I am not sure how that would be implemented.
Thank you very much,
Thomas :-)
It worked. I was sort of hoping to get around having to test each array element to increase speed. However, this seems to be the easiest method and it works perfectly.... Her is the final code:
for ($c=0; $c <count($s);$c++)
{
$helper_id = $s[$c][0]; // this is the car_id
for ($d=0; $d<21;$d++) // check 21 days
{
for ($h=0; $h<24;$h++) // every hour
{
$blocktime = date("d-m-Y H:00:00", mktime($h,0,0,date($datum_
for ($m = 0; $m<count($rented2);$m++) // loop through every recorded rental
{
if ($rented2[$m][1] == $blocktime and $rented2[$m][0] == $helper_id and !isset($jump))
{
$block[$helper_id][$blockt
$jump = $rented2[$m][2]; // set amount of hours rented
break;
}
} // end all rentals
if (!isset($jump))
{
$block[$helper_id][$blockt
} else {
$block[$helper_id][$blockt
$jump = $jump - 1;
if ($jump == 0) // length has been fullfilled -> next hour is free
{
unset ($jump);
}
}
} // end hours
} // End days
unset($jump); // unset jump so that the taken hours don't overlap to next car.
} // End cars
Hope this helps somebody. I will have a look at the array_walk functionality. Although I am not sure how that would be implemented.
Thank you very much,
Thomas :-)
You are using array_key_exist to find out if a VALUE exists in an array, rather than a KEY.
In your array, the following are valid keys:
Outside Array: "0", "1", "2" ...
Inside Array: "id", "start", "length"
So for example, you could do:
array_key_exists ($rented[$car_id], "start")
...but all this tells you is that the KEY named "start" exists in that array.
What you need to do is check the value of the array, and test it is the same as the $blocktime. The code you probably need is:
$rented[$car_id]['start'] == $blocktime