ARRAY-PHP
ARRAY-PHP
An array is a data structure that stores one or more similar type of values in a single
value. For example if you want to store 100 numbers then instead of defining 100
variables its easy to define an array of 100 length.
There are three different kind of arrays and each array value is accessed using an ID
which is called array index.
Numeric 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
Numeric Array
These arrays can store numbers, strings and any object but their index will be
represented by numbers. By default array index starts from zero.
LABWORK
<html>
<body>
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
</body>
</html>
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 otherwise it
would not return any value.
LABWORK
<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 />";
</body>
</html>
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.
LABWORK
<html>
<body>
<?php
$marks = array(
"mohammad" => array (
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"qadir" => array (
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
</body>
</html>
and eason[3]";
?>
returns
an
array.
Output:
It
allows Season are: summer, winter,
spring and autumn
you to
create
indexe
d,
associ
ative
and
multidi
mensio
nal
arrays.
3 PHP
PHP array_
array array_chunk ( arr <?php
on lse ] ) atan"=>"200000");
< splits print_r(array_chunk($salary,2)
? array );
into
p ?>
chunk
h s. By
Output:
p using
array_
Array (
chunk [0] => Array ( [0] => 550000
$ () [1] => 250000 )
s metho [1] => Array ( [0] => 200000
d, you )
a can )
l divide
array
into
many
parts.
4 PHP
PHP count(
int count ( mixed $array <?php
Output:
autumn
spring
summer
winter
6 PHP PHP array array_reverse ( a <?php
array_reverse( array_
)
revers rray $array [, bool $pre $season=array("summer","wi
e() serve_keys = false ] ) nter","spring","autumn");
functi $reverseseason=array_revers
on
return e($season);
s an foreach( $reverseseason as $
array s)
contai
ning {
eleme echo "$s<br />";
nts in }
revers
ed ?>
order.
Output:
autumn
spring
winter
summer
7 PHP PHP mixed array_search ( mi <?php
array_search() array_
searc xed $needle , array $h $season=array("summer","wi
h() aystack [, bool $strict = nter","spring","autumn");
functi false ] ) $key=array_search("spring",
on
searc $season);
hes echo $key;
the ?>
specifi
ed Output:
value
in an 2
array.
It
return
s key
if
searc
h is
succe
ssful.
8 PHP PHP array array_intersect ( <?php
array_intersec array_
t()
inters array $array1 , array $ $name1=array("sonoo","john
ect() array2 [, array $... ] ) ","vivek","smith");
functi $name2=array("umesh","son
on
return oo","kartik","smith");
s the $name3=array_intersect($na
inters me1,$name2);
ection
of two foreach( $name3 as $n )
array. {
In echo "$n<br />";
other
words }
, it ?>
return
s the Output:
match
ing sonoo
eleme smith
nts of
two
array.