Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> [php] Walking Through Multidimensional Arrays, Walk through multidimensional array with foreach and list
matak
post Apr 9 2007, 06:17 PM
Post #1


Super Member
*********

Group: Members
Posts: 412
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



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

CODE
$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

QUOTE

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..

CODE
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

QUOTE
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..
Go to the top of the page
 
+Quote Post
Imtay22
post Apr 9 2007, 06:20 PM
Post #2


Super Member
*********

Group: Members
Posts: 293
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
matak
post Apr 9 2007, 06:31 PM
Post #3


Super Member
*********

Group: Members
Posts: 412
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



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

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


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

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


Which makes your code easier to read, and simpler to manage wink.gif

EDIT:
Here you can see how arrays can be used to create id browsing..

This post has been edited by matak: Apr 9 2007, 06:33 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 28th August 2008 - 10:10 AM