problem looping through complex array

From: Date: Mon, 27 Jun 2005 23:33:37 +0000
Subject: problem looping through complex array
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12740@lists.php.net to get a copy of this message
Dear Smarty Smarties, This is a bit long. But, it's long because it's tough (I think so, anyway). I am having a problem finding a combination of {foreach} and {section} that will do what I want it to do. This may be due to the fact that I have never fully grasped {section}, unfortunately. Anyway... I have an array of projects. Each value in this array is an associative array containing data regarding that project. The final key is 'dirs', whose value is an array of associated filesystem directories for the project's files. In this array, the directories are the keys and the values are an array of files. Each file is an associative array of the file's information (filename, date created, description, etc.). The array I'm working with is below. I have the ability to alter the array structure if needed. The array is as it is because using this structure got me the closest to what I wanted. Part of the problem is that the keys of one of the arrays (the directories) are actually data I want. I'm not sure how to fix that. Here's the array: Array (4) 0 => Array (6) project_id => 4 companyname => Signal US Communications projectname => Project 4 description => blah blah datecreated => Jun 27th dirs => Array (0) 1 => Array (6) project_id => 2 companyname => Matt's Huge Company projectname => Project 2 description => blah blah datecreated => Jun 27th dirs => Array (0) 2 => Array (6) project_id => 3 companyname => Google, Inc. projectname => Project 3 description => blah blah datecreated => Jun 27th dirs => Array (0) 3 => Array (6) project_id => 1 companyname => Matt's Huge Company projectname => Project 1 description => blah blah datecreated => Jun 27th dirs => Array (2) test => Array (2) 0 => Array (4) file_id => 99 filename => foo.txt description => blah blah blah datecreated => 1119914453 1 => Array (4) file_id => 99 filename => bar.txt description => blah blah blah datecreated => 1119914453 test2 => Array (0) Here's the code I am using (very abbreviated): {foreach from=$projects item=project name=project} {$project.projectname} {foreach from=$project.dirs|@array_keys item=dir name=dir} {$dir} {foreach item=dirfiles from=$project.dirs} {foreach from=$dirfiles item=file name=file} {$file.filename} {/foreach} {foreachelse} dffasdfasdfasdfasdfasdf {/foreach} {foreachelse} This project has no directories. {/foreach} {foreachelse} There are no projects in the database. {/foreach} And here is the output. You'll notice that it works except that if you click on 'View Directories' on Project 1, and then click on the two 'View Files' links, the two files (foo.txt and bar.txt) show up in both the 'test' and 'test2' directories, whereas in the array they exist only in the 'test' directory. I think that's happening is that the dirs array is being looped through and all files are bring printed twice, but i'm not sure. http://24.210.245.115/project/smartyoutput.htm Can anyone help me? I'm kind of stuck here, and I don't want to have to compromise separation of presentation just to get around me not being able to wield Smarty appropriately. Again, feel free to offer simpler constructions of the array. I just need to output what is shown. Thanks a million, in advance. -- Matt Warden Miami University Oxford, OH, USA http://mattwarden.com This email proudly and graciously contributes to entropy.

« previous php.smarty.general (#12740) next »