Jump to content

Printing Multidimensional Arrays with C Style For Loop


juuuugroid

Recommended Posts

I'm trying to print a multidimensional array like I would in C.  I see that with php, I can use a foreach loop, but I'd love to use the syntax I'm used to.  The top example reads data into an array.  I just gets a directory reading and stores the size of a file and the last modified time of the flle.  It works fine. 

$fileInfo = array();

    if(substr($dir, -1) != "/") $dir .= "/";

    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
      if($entry[0] == ".") continue;
      if(is_dir("$dir$entry")) {
        $fileInfo[] = array(
          "size" => 0,
          "lastmod" => filemtime("$dir$entry")
        );
      } elseif(is_readable("$dir$entry")) {
        $fileInfo[] = array(
          "size" => filesize("$dir$entry"),
          "lastmod" => filemtime("$dir$entry")
        );
      }
    }
    $d->close();

Here is the part I'm having trouble with.  I just need to print it in a way that makes sense to me.  Can somebody show me how this works without using the foreach loop.  I'm confused.  Thanks for your time!  Not trying to be one of those guys that can't change his coding ways, but I seem to think there was a way to do this. 

for ($i=0; $i<sizeof($fileInfo); $i++){

    for ($j=0; $j < sizeof($fileInfol[0]); $j++){

        for($k = 0; $k < sizeof($fileInfo[0]); $k++) {
            
            //echo "3 ";
            print ("i$i j$j " . $fileInfo[$i][$j][$k] . "<br> ");


        }
    }
}
Link to comment
Share on other sites

The only thing I see wrong is how many loops you have. There's only one array to loop over. Use only one loop.

 

I see that with php, I can use a foreach loop, but I'd love to use the syntax I'm used to.

And I would love it if people learning a language would try to adapt to the language instead of forcing their own learned habits upon it.
Link to comment
Share on other sites

If you don't know just say it or don't reply.  I got it figured out.  The language was written to accept it both ways.  I'm not bending it to my will.  I stopped posting on forums years ago because of D-Bag answers like this.  It only took one post before I'm done posting again.  Ha! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.