User Name
Password
AppleNova Forums » Genius Bar »

PHP : loading a multi-dimensional file with a loop


Register Members List Calendar Search FAQ Posting Guidelines
PHP : loading a multi-dimensional file with a loop
Thread Tools
drewprops
Magnificent Basturdâ„¢
sagacious-d
 
Join Date: May 2004
Location: Atlanta
 
2005-01-30, 23:23

Okay.
I understand the beginner's lessons on ways to create multidimensional arrays, but I'm not as certain how to fill a multidimensional array with other arrays by using a looping statement (while/if). I've been playing with this for the last few hours but have yet to get it to work and know that I'm missing a simple step in loading the array. What am I doing wrong?

Here's an example:
Let's say that I have a simple array called $filename that has been populated with ten JPEG files' names. I want to refine my control of these files and categorize them by the first four characters of each file, so I create a loop and cycle through the contents of $filename and use it to help name elements in the multidimensional array...

Code:
$filename=array('arch_001.jpg','arch_002.jpg','arch_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; if ($i=0;$i<$countup;$i++){ $filecategory=substr($filename[$i],0,4);//grab the first four characters of the filename $filecat[$filecategory][$i]=$filename[$i];//stick into a multi-dimensional array using its category }

Steve Jobs ate my cat's watermelon.
Captain Drew on Twitter
  quote
drewprops
Magnificent Basturdâ„¢
sagacious-d
 
Join Date: May 2004
Location: Atlanta
 
2005-01-30, 23:33

...and already I spot a flaw in the logic of this loop.
  quote
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
drewprops
Magnificent Basturdâ„¢
sagacious-d
 
Join Date: May 2004
Location: Atlanta
 
2005-01-31, 20:06

D'oh!
I really, honestly WAS using a "for" in my attempts... but the logic that you're using is something that I'm going to spend the next little while unwrapping. I'm fairly positive that what you've shown me will help me tremendously! All I needed was a mental "foot up" to get over the wall. I'll let you know if, no, WHEN I'm able to make this function within my real code and not just this (poorly) cobbled together example. Thanks!!

Steve Jobs ate my cat's watermelon.
Captain Drew on Twitter
  quote
Gargoyle
http://ga.rgoyle.com
 
Join Date: May 2004
Location: In your dock hiding behind your finder icon!
 
2005-02-01, 07:57

Foreach is the king of processing arrays. Try this on for size...

[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'
);

foreach ($filename as $key => $value) {
$filecategory=substr($value, 0, 4); // Get category.
$filecat[$filecategory][] = $value;
}

[/php]


You dont need this counter anymore.
Code:
$countup=10;
using empty [] pushes your value onto the end of the array. If you want to preserve the original index use $filecat[$filecategory][$key] = $value instead.

Enjoy

Edit: Eeee the orange in the php code comments looks nasty, moved them out of the code section. Brad....

OK, I have given up keeping this sig up to date. Lets just say I'm the guy that installs every latest version as soon as its available!
  quote
Gargoyle
http://ga.rgoyle.com
 
Join Date: May 2004
Location: In your dock hiding behind your finder icon!
 
2005-02-01, 08:23

Also, you could add a notch to your programming belt by writing a recursive function.

[php]
function show_array_rec (&$somearray) {
if(is_array($somearray)){
foreach($somearray as $key => $value){
print $key." - ". show_array_rec($value);
}
} else {
print $somearray;
}
}
[/php]

  quote
drewprops
Magnificent Basturdâ„¢
sagacious-d
 
Join Date: May 2004
Location: Atlanta
 
2005-02-03, 23:05

Gargoyle, I have been mega-uber-busy at work but just remembered to run back in here and thank you as well. Tight code really is beautiful once you understand how it works... I'll be studying this stuff as soon as I get another good stretch of time to devote to it. I've spent acres of time searching the web for solutions to various problems and often times I find it. The few times I bother the gang here I'm rewarded with great answers.

Google is your friend, but nothing beats direct discussions!!

Obviously I'm building my own lightsaber, I mean, content management mini-system. I'm interweaving this with a bastard union of Movable Type and custom PHP pages. All great fun indeed~

Steve Jobs ate my cat's watermelon.
Captain Drew on Twitter
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools

« Previous Thread | Next Thread »

All times are GMT -5. The time now is 00:20.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2019, AppleNova