Jul 20, 2008

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

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

free web hosting

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

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

 

 

 


Reply

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

Reply

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

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : php walking multidimensional arrays multidimensional array foreach

  1. Arrays In Php - My Fourth PHP Tutorial (1)
  2. Php - Randomize Web Title - using arrays and the random function (5)
    PHP - Randomize Web Title with the rand function... Define the variables numbers we start at number
    0 CODE <?php $title[0] = "Web title 0"; // Title 0
    $title[1] = "Web title 1"; // Title 1 $title[2] = "Web title
    2"; // Title 2 $title[3] = "Web title 3"; // Title 3
    $title[4] = "Web title 4"; // Title 4 ?> now we use the rand()
    function to get a randomize the web title. rand(0, 4) means we start at 0 and end at 4 i can get
    0,1,2,3,4 one of those. ...



Looking for , php, walking, multidimensional, arrays, walk, multidimensional, array, foreach, list

Searching Video's for , php, walking, multidimensional, arrays, walk, multidimensional, array, foreach, list
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE