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($datum_split[2])-$start_offset+$d,date($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....


Thomas :-)

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

siliconbritConnect With a Mentor Commented:

I dont know what you mean by "what is missing is the outer key"  - Basically, you want to iterate through the array looking for cars that match the blocktime, or whatever else you want to specify.  I could write the whole code for you quite easily, but I dont have time, and the following code shows a very simple example of how to match using the example I gave in the previous quote - try it and see.


$rented[0]=array("id"=>182,"start"=>"13-03-2006 00:00:00","length"=>4.00);
$rented[1]=array("id"=>194,"start"=>"15-03-2006 08:00:00","length"=>4.00);
$rented[2]=array("id"=>194,"start"=>"15-03-2006 12:00:00","length"=>5.00);

$blocktime="15-03-2006 08:00:00";

for ($i=0; $i < count($rented); $i++) {

   if ( $rented[$i]['start'] == $blocktime ) {
      print "**MATCH: ";
   } else {
      print "NOMATCH: ";

   print "\$rented[$i]['start'] = ".$rented[$i]['start']."<br>";


My approach to the problem would be different, but its important that you solve your problem simply before using more complex or terse array functions.  Once you understand this code, and are able to modify it so that it produces the output you want, you might want to look at the php function array_walk [], or look at the whole array toolset at: []

Post back here with your progress.


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

krackltAuthor Commented:
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?

krackltAuthor Commented:
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($datum_split[2])-$start_offset+$d,date($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][$blocktime] = "taken"; // record start time as rented
                              $jump = $rented2[$m][2]; // set amount of hours rented
                        } // end all rentals      
                              if (!isset($jump))
                                    $block[$helper_id][$blocktime] = "free";

                                    } else {
                                    $block[$helper_id][$blocktime] = "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 :-)
All Courses

From novice to tech pro — start learning today.