FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Array in php





sonal12
Hello all,
Can anyone explain the working of Multidimensional array i PHP.I read fro w3schools but it is not clear to me.Please explain it i detail.
jmraker
It's basically it's like a normal array except instead of each item having a string, numeric or boolean value all or some of it's item values are an array. It's the array inside an array that makes it multidimensional. Arrays aren't too limited in how deep you can go, you can have an array in an array in an array in the main array. Many of the XML parsing functions return ahuge multidimentional array containing everything in the document that go as deep as the parser goes.

Code:
// Define a multidimensional array
$arr = array('names'=>array('fri', 'host', '.com'), 'numbers'=>array(123,234,345), 'one'=>array('a'), 'two'=>array('a', 'a'));
var_dump($arr);

$a = $arr['names'][1]; // $a = the value "host"

Code:
array(4) {
  ["names"]=>
  array(3) {
    [0]=>
    string(3) "fri"
    [1]=>
    string(4) "host"
    [2]=>
    string(4) ".com"
  }
  ["numbers"]=>
  array(3) {
    [0]=>
    int(123)
    [1]=>
    int(234)
    [2]=>
    int(345)
  }
  ["one"]=>
  array(1) {
    [0]=>
    string(1) "a"
  }
  ["two"]=>
  array(2) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "a"
  }
}
badai
did you try to visualize it? that's what make you go crazy.

another way to look at it, see you computer's folder.

inside your drive, you see some folders.

inside each folder, you can see some child folders.

inside each child folder, got some other folders.

this goes on and on.

array is exactly like that. each array can have another array in it. each child array can have another array. on and on.

each array don't have to have exactly the same number of child array, and some array can even do not have any child array, just a value. which if we go back to the computer's folder analogy, you may call it file.

just stop visualizing object that have more than 3 dimensions in your head. you will go crazy.
ankurthakur
Multi Dimensional Arrays are a Good way to work on Structured Data.

Some Examples :-
- Multi Level Menus using PHP
- Multi Level pages
- Storing Multiple Information under 1 variable (like storing telephone no, website, name, age under 1 single array item, say, user[0] and thereby storing information for multiple users)

Above users give you a fine working of that Wink
Aredon
Quote:

did you try to visualize it? that's what make you go crazy.


I always visualize arrays like tables, because that's essentially what they are. A single array would be 2 columns, one with labels and one with values. At least... that was my sudden realization when I finally understood arrays years and years ago. They're just tables where the key corresponds to the row with the information you're looking for!

Single-Dim array
array (table title)
0 | value
1 | value
2 | value
etc

from there just think of multi-dimensional arrays as columns within those cells.

Multi-Dim array
array(table title) = $array
0 | child array = $array[0]
--> 0 | value 1 = $array[0][0]
--> 1 | value 2 = $array[0][1]
--> 2 | value 3 * = $array[0][2]
1 | value = $array[1]
2 | child array = $array[2]
--> 0 | value = $array[2][0]
--> 1 | child array = $array[2][1]
-->--> 0 | value = $array[2][1][0]

lets say I wanted to find the 3rd value in the first child array (marked with *). My variable referance for that would be $array[0][2]. Essentially you're saying $table[row0][sub-row2].

Obviously the #s are the keys, and can also be associative. Hope this helps Smile. If not I can try to make a more graphical representation later.
campuspk
$employee_array[0] = "Bob";
$employee_array[1] = "Sally";
$employee_array[2] = "Charlie";
$employee_array[3] = "Clare";
silverdown
This is a old topic/post but to help out ill post a great example.
An array is a special variable that may store multiple values in one single variable.

This example code shows how to use text and integer based variables

Code:
<?php

$frihost = array("Frihostadmin"=>"admin of frihost", "Frihostuser"=>"user of frihost", "Frihostvisitor"=>"visitor of frihost", "Totalusers"=>100);

echo "Bondings is the head " . $frihost['Frihostadmin']  . " while the site has tons of people who are " . $frihost['Frihostuser'] . " as well as many " . $frihost['Frihostvisitor']  .  "  the site has way more then " . $frihost['Totalusers'] . " total memebers"  ;


?>


$frihost = variable

array(call-variable => value-returned) = list of values

then when you want to display one of the values

$frihost[call-variable]
inuyasha
Actually array in php is not really what it means in C or C++. It works much like a generalized list.
spring567
You can see more php examples.
Aredon
inuyasha wrote:
Actually array in php is not really what it means in C or C++. It works much like a generalized list.


Because we use C and C++ to define words. Cool
Fire Boar
Terminology ho! Arrays in PHP are known, in various other languages, as loads of things: hashes, dictionaries, maps, lists of pairs, etc. Whereas in most other languages, an array is something completely different to the PHP notion of array. Oh well.
Related topics
The .tpl mess
[php] array reference
PHP Mysql admin area? please help
Produce a array that is hard to sort for PHP
HEEEELP!!!!
Problem with Member system(or template) and $_GET[id]
-> and =>
Php while array problem
mysql - doubles
PHP Array, explode, count , access array problem.
Arrays?
PHP load array from $_POST
array diplay in a table using php
[PHP] Some beginner questions
Substr-like function for array
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.