| Getting infro from array
|
Marked

msg:4083793 | 4:24 am on Feb 20, 2010 (gmt 0) | I'm sure this is simplest thing ever... I have an array, I want to get info from using foreach(if possible). Using the print_r function, the array looks like this:
Array ( [Mm] => Array ( [0] => darkone.png [1] => forestone.png [2] => metalone.png [3] => parchmentone.png [4] => redwood.jpg ) [Nm] => Array ( [0] => ci_teal.jpg ) [Am] => Array ( [0] => back.jpg [1] => backvx.jpg [2] => backxp.jpg [3] => simplegreen.jpg [4] => simplered.jpg ) ) Basically what I am trying to do is list the first section of the array(Mn,Nm and Am), and then underneath list their values. Like so: Mn -darkone.png -forestone.png -metalone.png -parchmentone.png -redwood.jpg Nm -ci_teal.jpg And so on. They will actually be made into a select field in a form, but I can do that once i have access to them from the foreach loop or whatever. So I was wondering how to do this. I was trying to do it in a foreach loop but with no success. Thanks in advance, Mark.
|
Readie

msg:4083807 | 5:39 am on Feb 20, 2010 (gmt 0) | The syntax you're using for the array there is a bit wrong, see below:
<?php
$pics = Array( "Mm" => Array( 0 => 'darkone.png', 1 => 'forestone.png', 2 => 'metalone.png', 3 => 'parchmentone.png', 4 => 'redwood.jpg' ), "Nm" => Array( 0 => 'ci_teal.jpg' ), "Am" => Array( 0 => 'back.jpg', 1 => 'backvx.jpg', 2 => 'backxp.jpg', 3 => 'simplegreen.jpg', 4 => 'simplered.jpg' ) );
$head = array_keys($pics);
$i = 0; foreach($pics as $pic) { echo '<p>[' . $head[$i] . ']<br />'; $ii = 0; foreach($pic as $pi) { echo '- ' . $pi . '<br />'; } echo '</p>'; $i += 1; }
?> That covers what you're asking for I believe.
|
Readie

msg:4083829 | 7:36 am on Feb 20, 2010 (gmt 0) | $ii = 0; Delete this line by the way, was originally going about this a different way and it's un-needed
|
astupidname

msg:4083830 | 7:39 am on Feb 20, 2010 (gmt 0) | Good advice from Readie about the array syntax, but a small correction should be made to the looping. It would be bad practice to mix property access and indexable access and have them rely on each other matching up. Should be: $pics = Array( "Mm" => Array( 'darkone.png', 'forestone.png', 'metalone.png', 'parchmentone.png', 'redwood.jpg' ), "Nm" => Array( 'ci_teal.jpg' ), "Am" => Array( 'back.jpg', 'backvx.jpg', 'backxp.jpg', 'simplegreen.jpg', 'simplered.jpg' ) ); foreach($pics as $key => $val) { echo '<p>[' . $key . ']<br />'; foreach($val as $pi) { echo '- ' . $pi . '<br />'; } echo '</p>'; } |
|
|
Marked

msg:4083852 | 9:14 am on Feb 20, 2010 (gmt 0) | Thank you for you replies :) That isn't actually the syntax of the array. To get what I posted above, i used the following: print_r($array); The array is generated by using a function which uses opendir and readir and stores the directory names and image names into the array. Thanks guys, I got the foreach loop working. To save topic space, I'm also having another problem. Using the same array, is it possible to order all of the items in alphabetical order? For example, order Mn, Am, Nm so its An,Mn,Nm. And then their individual arrays into alphabetical order. Does anyone know how to do that? Thanks Mark.
|
astupidname

msg:4083857 | 9:43 am on Feb 20, 2010 (gmt 0) | Sure, check out php's array sorting functions, a few: $pics = Array( "Mm" => Array( 'forestone.png', 'metalone.png', 'parchmentone.png', 'darkone.png', 'redwood.jpg' ), "Nm" => Array( 'ci_teal.jpg' ), "Am" => Array( 'backvx.jpg', 'backxp.jpg', 'simplegreen.jpg', 'back.jpg', 'simplered.jpg' ) ); ksort($pics); //sorts array by it's keys, maintaining key/value relationship foreach($pics as $key => $val) { echo '<p>[' . $key . ']<br />'; sort($val); //sorts array by values, assigning new keys foreach($val as $pi) { echo '- ' . $pi . '<br />'; } echo '</p>'; } |
|
|
Readie

msg:4083931 | 3:09 pm on Feb 20, 2010 (gmt 0) | | but a small correction should be made to the looping. It would be bad practice to mix property access and indexable access and have them rely on each other matching up. |
| Agreed, I'm not in the habit of using foreach loops, and had forgotten that little nugget about $arr as $key => $value
|
|
|