Posts Tagged ‘The php count() Function’

PHP Arrays

What is an Array?

An array is a special variable, which can hold more than one value at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

  $cars1=”Volvo”;
$cars2=”BMW”;
$cars3=”Toyota”;

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is to create an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.

 

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Indexed array – An array with a numeric index. Values are stored and accessed in linear fashion
  • Associative array – An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array – An array containing one or more arrays and values are accessed using multiple indices

NOTE: Built-in array functions is given in function reference PHP Array Functions

 

Create an Array in PHP

In PHP, the array() function is used to create an array:

  array();

PHP Indexed Arrays

These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero.

Example

Following is the example showing how to create and access numeric arrays.

Here we have used array() function to create array. This function is explained in function reference.

<html>
<body>
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
echo “Value is $value <br />”;
}
/* Second method to create array. */
$numbers[0] = “one”;
$numbers[1] = “two”;
$numbers[2] = “three”;
$numbers[3] = “four”;
$numbers[4] = “five”;

foreach( $numbers as $value )
{
echo “Value is $value <br />”;
}
?>
</body>
</html>

This will produce following result:

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five

Get The Length of an Array – The count() Function

The count() function is used to return the length (the number of elements) of an array: Example:

  <?php
$cars=array(“Volvo”,”BMW”,”Toyota”);
echo count($cars);
?>

Output Result:

  3

Loop Through an Indexed Array

To loop through and print all the values of an indexed array, you could use a for loop, like this: Example:

  <?php
$cars=array(“Volvo”,”BMW”,”Toyota”);
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
{
echo $cars[$x];
echo “<br>”;
}
?>

Output Result:

  Volvo
BMW
Toyota

 

Associative Arrays

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

NOTE: Don’t keep associative array inside double quote while printing otheriwse it would not return any value.

Example

<html>
<body>
<?php
/* First method to associate create array. */
$salaries = array(
“mohammad” => 2000,
“qadir” => 1000,
“zara” => 500
);

echo “Salary of mohammad is “. $salaries[‘mohammad’] . “<br />”;
echo “Salary of qadir is “.  $salaries[‘qadir’]. “<br />”;
echo “Salary of zara is “.  $salaries[‘zara’]. “<br />”;

/* Second method to create array. */
$salaries[‘mohammad’] = “high”;
$salaries[‘qadir’] = “medium”;
$salaries[‘zara’] = “low”;

echo “Salary of mohammad is “. $salaries[‘mohammad’] . “<br />”;
echo “Salary of qadir is “.  $salaries[‘qadir’]. “<br />”;
echo “Salary of zara is “.  $salaries[‘zara’]. “<br />”;
?>
</body>
</html>

This will produce following result:

Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low

Loop Through an Associative Array

To loop through and print all the values of an associative array, you could use a foreach loop, like this: Example:

  <?php
$age=array(“Peter”=>”35″,”Ben”=>”37″,”Joe”=>”43”);

foreach($age as $x=>$x_value)
{
echo “Key=” . $x . “, Value=” . $x_value;
echo “<br>”;
}
?>

Output Result:

  Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

Multidimensional Arrays

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Learn php Array Programming in kurdish at kurdsofts.net

Example

In this example we create a two dimensional array to store marks of three students in three subjects:

This example is an associative array, you can create numeric array in the same fashion.

<html>
<body>
<?php
$marks = array(
“mohammad” => array
(
“physics” => 35,
“maths” => 30,
“chemistry” => 39
),
“qadir” => array
(
“physics” => 30,
“maths” => 32,
“chemistry” => 29
),
“zara” => array
(
“physics” => 31,
“maths” => 22,
“chemistry” => 39
)
);
/* Accessing multi-dimensional array values */
echo “Marks for mohammad in physics : ” ;
echo $marks[‘mohammad’][‘physics’] . “<br />”;
echo “Marks for qadir in maths : “;
echo $marks[‘qadir’][‘maths’] . “<br />”;
echo “Marks for zara in chemistry : ” ;
echo $marks[‘zara’][‘chemistry’] . “<br />”;
?>
</body>
</html>

This will produce following result:

Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39

 

Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function!

Next Chapter »

Designed for Mytoptutorials.com