pTutorial

New way to learn PHP

PHP 5 Array Manipulation

Creating an array

We can create and initialize the array using three ways, that all are following below.

Simple c language method

$student [0] = "John";
$student [1] = "Smith";
$student [2] = "Mr. Brown";

Another C language method

$student [ ] = "John";
$student [ ] = "Smith";
$student [ ] = "Mr. Brown";

Using array function

$Student = array ( "john", "smith", "brown");

You can use any of three method to create and initialize the array but last one is the better option to create array because only one variable used here.

Example

 <?php
 
$student = array("John","Smith","Harry");
$roll_number[0] = 1; 
$roll_number[1] = 2;
$roll_number[2] = 3;  

echo $student[0];
echo $roll_number[0];

 
?>

Output

John1

We have created two array using different method and assign three value each.

Access the first element of both array.

You can use loop to access all the element of array.

Arrays structure

You can see the structure and values of any array using print_r () function.

print_r($student);

Creating and accessing an array

<?php

$student=array("John","Smith","Harry");

echo "Structure of an array"."
"; print_r($student); echo "
"; foreach( $student as $value ) { echo "Name of student: $value"."
"; } ?>

In above example we have created an array named student and assign three values.

We have used print_r function to see the structure of the array.

And at last access all the array element using foreach loop.

Modifying arrays

Arrays can be changed at any time in the script, just as variables can. The individual values can be changed, elements can be added or removed.

Syntax of modifying array

$student [2] = "Mr. Black";

Another way of modifying array

$student [] = "Mr. Black";

According to the above syntax value will be added at last of array.

Example of modifying an array

<?php

$student=array("John","Smith","Brawn");
echo "Before modification"."
"; foreach( $student as $value ) { echo "Name of student: $value
"; } $student[1]="Mr. black"; echo "after modification"."
"; foreach( $student as $value ) { echo "Name of student: $value
"; } ?>

student is array type variable that hold the three element. We can check the variable by printing the complete array using foreach loop.

According to the line number 9 modify the second element of the array Smith to Mr. black.

After modification print again complete array and see the result.

Removing values from arrays

Sometimes you need to remove a value from an array and sometime you need to remove complete array. You can use unset() function to remove array. It takes one argument that is array variable.

Syntax of remove complete array

unset( $student );

Syntax of remove element of array

unset($student [1]);
<?php

$student = array("John","Smith","Harry");

unset($student[2]);

echo $student[1];

//echo $student[2];not accessible

?>

After removing the array element you cannot access that element. If you access that element, error message will be displayed.

Another way to remove array element

$student [1] = " ";

Example of removing values from an array

<?php

$student=array("John","Smith","Brawn");
echo "Before modification"."
"; foreach( $student as $value ) { echo "Name of student: $value
"; } $student[1]=" "; echo "after modification"."
"; foreach( $student as $value ) { echo "Name of student: $value
"; } ?>

If you are using above method it's only replace the current value to null value and you can access null value also so that there is no error message.


QUOTES OF THE DAY

The heart of melody can never be put down.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER