View Single Post
MCQ
Veteran Member
 
Join Date: May 2004
Location: NY
Send a message via MSN to MCQ  
2005-01-31, 00:17

What loop?

Ideally, you probably want to index it by category, then the current size of the category + 1 to insert the new element. I have code below that should fix it. It also prints out the array - one version is built in, the other is cobbled up from something I found online.

Note - If you use $filecat[$filecategory][$i], it should still work - just that your indexing will be 0-9 as you go through the array. So, that one change from "if" to "for" should get things working. However, print_r and the other code will still print it okay.



Code:
#!/bin/php <?php $filename=array('arch_001.jpg','arch_002.jpg','arc h_003.jpg','logo_001.jpg', 'logo_002.jpg','iden_001.jpg','clad_001.jpg','clad _002.jpg','firt_001.jpg','firt\ _002.jpg'); $countup=10; for ($i=0;$i<$countup;$i++){ $filecategory=substr($filename[$i],0,4);//grab the first four characters of the fi\ lename // find out count of this category's array $test = count($filecat[$filecategory]); $filecat[$filecategory][$test]=$filename[$i];//stick into a multi-dimensional arra\ y using its category } // built in php function to dump array out print_r(array_values($filecat)); // self-written function to write array out // credit: http://www.desilva.biz/arrays/multidimen.html for information on how t\ o do it echo "Manually outputting array:\n"; foreach ($filecat as $filetype =>$file) { echo "'$filetype'\n"; foreach($file as $filename) { echo " '$filename'\n"; } } ?>
  quote