homepage Welcome to WebmasterWorld Guest from 54.166.122.65
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Getting infro from array
Marked

5+ Year Member



 
Msg#: 4083791 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4083791 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4083791 posted 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

5+ Year Member



 
Msg#: 4083791 posted 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

5+ Year Member



 
Msg#: 4083791 posted 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

5+ Year Member



 
Msg#: 4083791 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4083791 posted 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved