Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with PHP Arrays

In this article we will discuss about the array processing for PHP. Array is a special type of variable that stores more than one data at a time. We can also say that array is nothing but a variable that stores more data with same data types.

So it is necessary for every programming language. Most of the problem is solved by the array processing. Generally we can use the array in all types of data type it may be char, float, string, integer but one point should be remembered that array index value must be provide with “[index]” as integer format.

Array is a collection of variables indexed and bundled into a single, easily referenced super variable that offers an easy way to pass multiple values between line of code and function. Many built-in PHP environment variables are in the form of arrays like an example $_SEESION which contains all the variable names and values. Almost in any type of situation that calls for a number of pieces of data to be packaged and handled as one is appropriate for a PHP array. Generally the PHP support three types of array and the array is like that following order:

  • Numerical Array
  • Multidimensional Array
  • Associative Array

Advantages of PHP Array:

  • We set index value as Integer and also String format like that $sname["mrbool"] = "tutorial";
  • This index value normally starts with integer format as “0” and string format is totally our choice that is used in associative array.
  • Index value is started with “0” as we input data with starting loop “0” and also output value is started with “0”.
  • Habitually when we use Session or Cookies programming then that system supports associative array concept. Like $_SESSION['view']=2;
  • It is very easy to remember that index data as we use integer array or use associative array.
  • We can use it just as a real array.
  • It is used like list, dictionary, collection, stack, and queue and probably other also.
  • We can create different types of sorting process using Array.
  • It is also supported to change key and value for different reasons.
  • We can also merge two arrays with PHP function.

PHP Array Syntax:

The simplest way to create an array is to act as though a variable is already an array and assign a value into it, like this:

$srimani_array[1]=”Welcome to Computer Educational Institution..”;

If $srimani_array was an unbounded variable or it may be bounded to non-array variable. Before this process it will now be a variable process. PHP language-construct array() is used to create an array. See example below:

array( [key =>] value
   …
  )

Generally it is also two parts one is key and other is value:

  • key: This key may be an integer or string format.
  • Value: A value can be of any PHP type

Examples

$arr = array(“cei” => “maa”,10 => true);
echo $arr["cei"]; this will print maa
echo $arr[10]; this will print 1

If we provide the brackets with no key specified, then the maximum of the existing integer is taken as key with value indices +1

Now we are discussing different types of array which is supported via PHP through the following process.

Process to create Numeric Array:

Numeric array is such type that index value starts with numeric format and we can also use very general format similar with other language array format.

Listing 1: Sample showing array format

<html>
 <head>
  <title>Changing array values</title>
 </head>
 <body>
 <?php
  $arr = array( "Computer ", "Educational ", "Institution" );
  echo( $arr[0] . $arr[1] . $arr[2] . "<hr>" );
  $arr[0] = 99;
  $arr[1] = 12.5;
  $arr[2] = $arr[0] + $arr[1];
   echo( "$arr[0] + $arr[1] = $arr[2]" );
 ?>
 </body>
</html>

Process to create Different Sorting using Numeric Array:

Another type of example is given below that defines how to execute sorting data of the array. We know that sorting process is very long program but PHP support array() function. Using this procedure we can find out sorting data using array.

Listing 2: Sample showing sorting array

<?PHP
$cei = array();
$ cei []="21";
$ cei []="18";
$ cei []="180";
$ cei []="46";
sort($numbers); <-  This function is responsible for sorting data

print $ cei [0] . " ";
print $ cei [1] . " ";
print $ cei [2] . " ";
print $ cei [3];
?> 

Output is given below:

18 21 46 180

Process to create Multidimensional Array:

The array examples we have looked at have all been one-dimensional with only one level of bracketed keys. PHP can easily support multidimensional(more array is supporting one array) array with random number of keys and just as with one dimensional array there is no need to declare our intentions in advance. The first reference of an array variable can be assigned like this.

$cei_array[1][2][3]=”Computer Educational Institution”; 

This is three dimensional arrays with successive keys that happen in this case to be three successive integers. Actually in our conclusion of array as multidimensional makes it more puzzling than they need to be. Just remember that the value which is stored in the arrays can themselves be array, just as they can be string or number. The multiple index syntax in the preceding example is simply concise way to refer to an array that is stored with a key of 1 in $cei_array which in turn has array store it and so on.

Point should be remembered that we can have different depths of reference in the difference parts of array like the following way:

Listing 3: Sample showing array depth

<?php
$cei = array ( "Software"=>array ( "C", "C++", "Java" ), 
"Hardware"=>array ( "Electronics" ), 
"Multimedia"=>array ( "Photoshop", "Flash", "Maya" ) ); 
?>

The output will be look like this:

Array ( [Software] => Array ( [0] => C [1] => C++ [2] => Java ) [Hardware] => Array ( [0] => Electronics) [Multimedia] => Array ( [0] => Photoshop [1] => Flash [2] => Maya) )

Process to create Associative Array:

Associative array creation is very easy. Because we already know that this array also support string index value which is totally user defined. So when we use this associative array then we have to remember that the associative array indexing is totally our choice.

The following example shows that creation of Associative Array.

Listing 4: Sample showing associative array syntax

<?php

$course = array ("C++"=>"Rs.1400", "Java"=>"Rs.1600","PHP"=>"Rs.2400","Netwoking"=>"Rs.3000");

?>

Listing 5: Sample showing Example of Associative Array

<?PHP
//Using For Each Looping round an Associative Array.
$sname = array();
$sname["Subrata"] = "Srimani";
$sname["Sukanta"] = "Sreemany";
$sname["Sushmita"] = "Das";
foreach ($sname as $first_name => $surname) {
print "Key = " . $first_name . " Value = " . $surname . "<BR>";
print "First name = " . $first_name . " Second name = " . $surname . "<BR>";
}?>

Output

Key = Subrata Value = Srimani
First name = Subrata Second name = Srimani
Key = Sukanta Value = Sreemany
First name = Sukanta Second name = Sreemany
Key = Ayantika Value = Roy
First name = Sushmita Second name = Das

Process to create Different Sorting using Associative Array:

Specially Sorting are of two types, one is called ascending order and others is called descending where ascending means sorting data A-Z and descending order means Z-A order.

Here we can develop sorting the array using value and keys.

  • asort() - This is Special type of sorting that sorts the data using value.
  • ksort() - This is Special type of sorting that sort the data using key.

Following is a sample code:

Listing 6: Sample code for Different sorting procedure

<?php
//Different sorting procedure using Associative Array
$stu_marks = array ("Sushmita"=>"535", "Payal"=>"670", "Ayantika"=>"605","Rahul"=>"170","Olivia"=>"570");

print("Before asort: <br>");

foreach ($stu_marks as $marks=>$no) {
print("There are $no ${marks}s.<br>");
}
asort($stu_marks);

print("After asort: <br>");

foreach ($stu_marks as $marks=>$no) {
print("There are $no ${marks}s.<br>");
}

print("Before ksort: <br>");

foreach ($stu_marks as $marks=>$no) {
print("There are $no ${marks}s.<br>");
}

ksort($stu_marks);

print("After ksort: <br>");

foreach ($stu_marks as $marks=>$no) {
print("There are $no ${marks}s.<br>");
}
?> 

Output

Before asort:
There are 535 Sushmitas.
There are 670 Payals.
There are 605 Ayantikas.
There are 170 Rahuls.
There are 570 Olivias.
After asort:
There are 170 Rahuls.
There are 535 Sushmitas.
There are 570 Olivias.
There are 605 Ayantikas.
There are 670 Payals.
Before ksort:
There are 170 Rahuls.
There are 535 Sushmitas.
There are 570 Olivias.
There are 605 Ayantikas.
There are 670 Payals.
After ksort:
There are 605 Ayantikas.
There are 570 Olivias.
There are 670 Payals.
There are 170 Rahuls.
There are 535 Sushmitas.

Conclusion:

So from the above example we can easily determine that the concept of the PHP array is very wonderful for our modern programming technology. Without array we can’t think, because the programmers face lot of trouble at the time of developing application with various data. Though PHP is Server side programming language but at the time of sorting, searching especially in database, session, cookies as well as other programming we must use array. It is really helpful of PHP associative array. Through this associative array processing we can solve more and more complicated problem that was very complicated earlier.

So we can conclude our discussion by saying that PHP Array is no doubt a important component to be learnt carefully.

Hope you liked the article, see you next time.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login