Udjuni.com

udjuni.com search:




Arrays in PHP


In the world of programming, you normally use two kinds of variables, one is what is called scalar variables which can only store one value at any given time. Another kind of variable is what is called arrays which are indexed storage location that are able to keep multiple values. By default, the array index starts at 0.


Creating Arrays


To create an array, you can either use the array function array() or the array operator array_name[]. Bellow are examples of how to create arrays:
$firstnames = array(“Nelson”, “Daniel”, “Harper”, “Jennifer”); 

Another way to achieve the same results is to use the following notation or format:
$firstnames[] = “Nelson”;
$firstnames[] = “Daniel”;
$firstnames[] = “Harper”;
$firstnames[] = “Jennifer”;

Both these approaches will create an array with four values and indexing starting at zero and ending with 3.

Associative arrays


With this kind of arrays, instead of using the numeric index, they use named keys. The format is to use the key and the ( => ) symbol, then followed by the value to be associated with the key. For example:
$teacher = array(
“name” = “Mike Ndango”,
“position” = “auto-electrician”,
“age” = “30”);


Retrieving array values


For ordinary arrays, all you have to do to retrieve the value is to use the index position. For example to get the name Nelson from the $firstnames array, you do the following:
echo $firstnames[0]; 

because Nelson is the first item in the array, by default it is going to be stored in index position 0. Retrieving values from an associative array is also very similar, except, instead of using numeric index positions, you use the name key to retrieve the values like:
echo $teacher[name]
to retrieve the teachers’ names.



RELATED ARTICLES:


arrays


Array related PHP Functions
The PHP language has a lot of functions that are used to populate, retrieve and manipulate array variables. Bellow are some of those functions, we will be using some of them as we continue to learn. Functi

PHP Variables
Variables In PHP, unlike other programming languages you can declare a variable or allocate variable storage space without worrying about the type of data that will be stored in it. Thus values are used when data in your script becomes available and can change during the lifetime o

Solving linear equations with two variables
In the last posting titled "Basic Linear equations", we covered the basics of solving an equation with one "unknown". There are rules that govern linear equations that we did not cover in the last posting. I will be covering those rules here before we move on to linear equations with two unkn

Exponents of Variables


Of Variables


Java variables and data types
A variable is defined as a named piece of memory that a java program uses to store data. Each of these storage pieces can only store data of a certain type. In other words, if you defined as variable of type integer, that variable can only hold inte

Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed