juuuugroid Posted June 9, 2013 Share Posted June 9, 2013 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> "); } } } Quote Link to comment https://forums.phpfreaks.com/topic/278954-printing-multidimensional-arrays-with-c-style-for-loop/ Share on other sites More sharing options...
requinix Posted June 9, 2013 Share Posted June 9, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278954-printing-multidimensional-arrays-with-c-style-for-loop/#findComment-1434948 Share on other sites More sharing options...
juuuugroid Posted June 9, 2013 Author Share Posted June 9, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/278954-printing-multidimensional-arrays-with-c-style-for-loop/#findComment-1434995 Share on other sites More sharing options...
Barand Posted June 9, 2013 Share Posted June 9, 2013 Just as you would in C echo '<pre>'; for ($i=0, $k=count($fileinfo); $i < $k; $i++) { printf ("%3d\t%8d\t%-15s\n", $i, $fileinfo[$i][0], $fileinfo[$i][1]); } echo'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/278954-printing-multidimensional-arrays-with-c-style-for-loop/#findComment-1435003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.