Jump to content
xisto Community
Sign in to follow this  
matak

[php] Walking Through Multidimensional Arrays Walk through multidimensional array with foreach and list

Recommended Posts

If you like PHP arrays like i do you probably wondered how to walk through multidimensional arrays. You can construct them maybe from your database or flat file, and output them with simple two functions foreach, and list..

Let's say we have something like this

 

$information = array (						'id0001' => array ('ivan', 'ivanovich', 'm', '24'),						'id0002' => array ('marko', 'markovich', 'm', '21'),						'id0003' => array ('ana', 'anich', 'z', '35'));

that string represents multidimensional array.

 

Multidimensional array is array of arrays. It offers much more efficient way of storing similar information in one string. Our string $information contains array of ID's, and every ID contains array of user information for that ID. Like, name, lastname, gendre, and age. Now for us to use this construct we first set our ID's as $id string using foreach statement, and then asociate $name, $lastname, $gendre, $age strings for each value in array based on it's ID with list function. And then just to see the result we add echo function

 

PHP Net on List()

 

Description

void list ( mixed varname, mixed ... )

 

Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.


Here is the code we use for that..

 

foreach ($information as $id) {		list ($name, $lastname, $gendre, $age) = $id;	echo 	'<b>Name: </b>' .ucfirst($name). 		' <b>Lastname: </b>' .ucfirst($lastname). 		' <b>Gendre: </b>' .strtoupper($gendre). 		' <b>Age: </b><i>' .$age. '</i><br>';}

Output is this

 

Name: Ivan Lastname: Ivanovich Gendre: M Age: 24

Name: Marko Lastname: Markovich Gendre: M Age: 21

Name: Ana Lastname: Anich Gendre: Z Age: 35


You can see some functions here that are maybe unknown to you like ucfirst(). Here's what they do..

 

ucfirst() capitalizes the first letter in sentence. If you maybe think that names are going to be like Nikolai Dmitrievich Tolstoy maybe it would be smart to use ucwords() which capitalizes the first letters in all words of string.

 

strtoupper() outputs all letters to uppercase (vice versa is strtolower() )

 

I didn't put the sample or dl link, couse i think it is quite simple, but if you want it, just post here and i'll make one..

Hope this helps..

Share this post


Link to post
Share on other sites

Nice tutorial, Matak! I heard of these arays but never really took the time to figure out what they were. I might use them in my site. I am a fast reader.

Share this post


Link to post
Share on other sites

I really don't know how to easily explain what arrays are. They are like series of objects with same information you can use to store data in. Maybe the simplest way to explain what array is is just to write couple of constructs with plain variables(strings) and then use array for the same. Let's say you want to write many strings that you wish to include with your template.. You can either write something like this

$website1 = "website1.php";$website2 = "website2.php";$website3 = "website3.php";...

Which as you can is is quite annoying, or just write it like array

$websites  = array ("website1.php", "website2.php, "website2.php");

Which makes your code easier to read, and simpler to manage ;)

EDIT:
Here you can see how arrays can be used to create id browsing..
Edited by matak (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.