Search Multidimensional array

Posted on 2006-03-27
Last Modified: 2010-08-05
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 :-)

Question by:kracklt
    LVL 11

    Expert Comment


    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


    Author Comment

    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?

    LVL 11

    Accepted Solution


    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.


    Author Comment

    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 :-)

    Featured Post

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now