View and vote on the article here: A PHP coders diary Part2
A PHP coders diary Part2| Category | | | Summary | | | Body | Part 2 - Fun with arrays
This time, I want to show you some techniques used to process data, especially those involving arrays, the content shows up as follows:
1. Content
2. array()
3. Indexes
4. Referencing
5. "Variable variables" meet arrays
1. Content
But beware! If you come along you might learn something. :)
So lets just headstart in and take a look at a simple problem..
We want to:
"show a list of names"
Well, this task may seem trivial, but still i find it very usefull to present some different techniques of useing arrays.
2. array()
So we go ahead and define an array as follows:
$arr=array("Tom","Tim","Tina","James");
foreach($arr as $a) echo $a."
";
The above code will simply present a list of the names contained inside the array $arr we just defined.
But array(); can do more!
3. Indexes
Suppose you want to have a "reference" to a name, in example a short form, abbreviation or identifiaction number, which you could use to easily "find" the arrays data.
In this case you would use an "index", specified by the => operator
$arr=array("T1"=>"Tom","T2"=>"Tim","T3"=>"Tina","J1"=>"James");
foreach($arr as $a) echo $a."
";
This example shows the use of indexes, so lets get to the reason you would want to use an index anyway..
4. Referencing
The simple theory behind "referencing" is demonstrated here:
ARRAY_NAME
1. Tom
2. Tim
3. Tina
4. James
ARRAY_GENDER
1. male
2. male
3. female
4. male
Probably the most important point in referencing is that it exists only in the programmers head..
The manifestation of a reference inside the code can have 100 faces, some of them shown here:
$arr=array("T1"=>array("Tom","male"),"T2"=>array("Tim","male"),"T3"=>array("Tina","female"),"J1"=>
array("James","male"));
Very simple, as an array level...
$genders=array("m"=>"male","f"=>"female");
$arr=array("T1"=>array("Tom","m"),"T2"=>array("Tim","m"),"T3"=>array("Tina","f"),"J1"=>array("James","m"));
Or more complex in a 2nd array, which would need to be additionally "referenced to" by an algorithm (code), in example:
foreach($arr as $a) echo $a[0]." ".$genders[$a[1]]."
";
The above line just runs through the array $arr, echoes the value of $genders thats asociated through every 2nd element in each subarray, using the index (the "m" or "f" in $arr).
You could go even further and create indexes for the subarrays as follows:
$genders=array("m"=>"male","f"=>"female");
$arr=array("T1"=>array("name"=>"Tom","gender"=>"m"),"T2"=>array("name"=>""Tim","gender"=>"m"),"T3"=>array("name"=>
""Tina","gender"=>"f"),"J1"=>array("name"=>""James","gender"=>"m"));
foreach($arr as $a) echo $a["name"]." ".$genders[$a["gender" ]]."
";
5. "Variable variables" meet arrays
Intro: A "Variable variable" is used like this:
$test="asdf";
$asdf="test";
echo $$test;
The above code will output "test", since "$$test, uses the value of $test as the name of the variable to output" :)
Imagine you have some more Information that you want to reference to, maybe the users favourite color:
COLORS
1. red
2. green
3. blue
$genders=array("m"=>"male","f"=>"female");
$g="green";
$r="red";
$b="blue";
$arr=array("T1"=>array("name"=>"Tom","gender"=>"m","color"=>"r"),"T2"=>array("name"=>"Tim","gender"=>"m","color"=>"b"),
"T3"=>array("name"=>"Tina","gender"=>"f","color"=>"g"),"J1"=>array("name"=>"James","gender"=>"m","color"=>"b"));
This setup may look a bit strange at the first glance, but remember its only an example:
foreach($arr as $a) echo $a["name"]." ".$genders[$a["gender" ]]." ".${$a{"color"]}."
";
All we added here was ${$a{"color"]}, which is a "Variable Variable", whichs name is given by an array (thats why you need the {} around it)
Written by NoImNot (30 October 2005)
NoImNot is a member of KB Tutorial Writing
Other articles from NoImNot:
* A PHP coders diary (Part 1)
* Rebuild a corrupted Partition Table the gpart/fdisk way
-----
{BACKLINKS(info=>hits|user,exclude=>SandBox,include_self=>0,noheader=>0)}{BACKLINKS} |
|
This article was imported from the CyberArmy University site. (original author: PixieLuv)
There are no replies to this post yet.
|