PHP Array

Array:

  • Array is collection heterogeneous Data┬átype.
  • php is loosely type language that’s way we can store any type of values.
  • Array is collection of element each element is combination of element key values.
  • By default 1st element key is 0 last element key is total number of element – 1.
  • Difference between normal variable and array is normal variable can store single value.array can store collection of values.
  • Using array function we can create array.Using print_r function we can print all elements with key and values.

Array are of two Type:

1.Numeric Array:

  • If we create any element with numeric key comes under numeric element.

2.Associative element:

  • Element with string key come under associative element.
Example for associative array
<?php
$arr=array();
$arr[0]=10;
$arr[1]=’scott';
$arr[2]=’alex';
echo $arr[2];
?>

Function in array are as follows:

count array_combine
sort rsort
asort arsort
ksort krsort
array_push array_pop
array_shift array_unshift
extract explode
array_search implode
array_slice in_array
Simple program for array
<?php
$numbers=array(100,200,300);
$cities=array(‘Capital of Nation’=>’Delhi’,’Capital os State’=>’Mumbai’,’My City’=>’Nashik’);
print_r($numbers);
print_r($cities);
?>
Program for an associative array
1. Display the elements of an array along with the keys
2. Display the size of an array
3. Delete an element from an array from the given key/index
4. Reverse the order of each element’s key-value pair
5. Traverse the elements in an array in random order

Array.php
<?php
include “array.html”;
$a=array(‘Capital of Nation’=>’Delhi’,’Capital of State’=>’Mumbai’,’My City’=>’Pune’);
$value=$_GET[‘ch’];
switch($value)
{ case ‘1’:echo ”
The input array is:”;
print_r($a);
break;
case ‘2’:$cnt=count($a);
echo “The size of an array is:$cnt”;
break;
case ‘3’:echo ”
The input array is:”;
print_r($a);
$remove=array_splice($a,2,2);
echo ”
The array after deletion is:”;
print_r($remove);
break;
case ‘4’:echo ”
The input array is:”;
print_r($a);
$reverse=array_flip($a);
echo ”
The Reverse order of each elements key-value pair is:”;
print_r($reverse);
break;
case ‘5’:echo ”
The input array is:”;
print_r($a);
shuffle($a);
echo ”
Traversed elements in random order are:”;
print_r($a);
break;
}
?>

Array.html

Operations on an associative array

Select Option:
Display the elements of an array along with the keys
Display the size of an array
Delete an element from an array from the given key/index
Reverse the order of each element’s key-value pair
Traverse the elements in an array in random order
Program for multidimensional array

marray.html

MULTIDIMENSIONAL ARRAY

Enter the Row:Enter the Column:

marray.php <?php
include “marray.html”;
$r=$_GET[‘var1′];
$c=$_GET[‘var2′];
$r0=array(‘a’,’b’,’c’);
$r1=array(‘d’,’e’,’f’);
$r2=array(‘x’,’y’,’z’);
$multi=array($r0,$r1,$r2);
$value=$multi[$r][$c];
echo “The input array is:”;
print_r($multi);
echo “The element at $r row and $c column is:$value”;
echo “Array after deletion of element $value is:”;
unset($multi[$r][$c]);
print_r($multi);
?>

Program For search element from Array

searcharray.html

SEARCH ELEMENT FROM ARRAY

Enter the value to be searched:

searcharray.php <?php
include “searcharray.html”;
$a=array(‘Capital of Nation’=>’Delhi’,’Capital of State’=>’Mumbai’,’My City’=>’Pune’);
$value=$_GET[‘value’];
echo”
The input array is:
“;
print_r($a);
$k=in_array($value,$a);
if($k==1)
echo “The element $value is in the array”;
else
echo “The element $value is not in the array”;
$k=array_search($value,$a);
echo “The searched key of $value using array_search() is:$k”;
?>

Leave a Reply

Your email address will not be published. Required fields are marked *