0% found this document useful (0 votes)
9 views

ARRAY-PHP

array in php
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

ARRAY-PHP

array in php
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PHP - Arrays

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);

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 the 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

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 />";

/* 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 the 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

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
),

"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 the following result −


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

S FUNCTIO DESC SYNTAX EXAMPLE


L N NAME RIPTI
N ON
O
1 PHP
PHP array array ([ mixed $. <?php
array()
array() .. ] ) $season=array("summer","wi
functio nter","spring","autumn");
n echo "Season are: $season[0],
creates $season[1], $season[2] and $s

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.

2 PHP array array_change_k <?php


PHP array_ ey_case ( array $arra
array_c change y [, int $case = CASE_ $salary=array("Sonoo"=>"55
_key_c LOWER ] ) 0000","Vimal"=>"250000","R
hange_ ase() atan"=>"200000");
key_ca functio
print_r(array_change_key_cas
n
se() change e($salary,CASE_UPPER));
s the
?>
case of
all key
Output:
of an
array.
Array ( [SONOO] => 550000
[VIMAL] => 250000 [RATAN]
Note: It => 200000 )
change
s case ray ( [SONOO] => 550000
of key [VIMAL] => 250000
only. [RATAN] => 200000

3 PHP
PHP array_
array array_chunk ( arr <?php

array_c chunk ay $array , int $size [, b $salary=array("Sonoo"=>"55


()
hunk() functi
ool $preserve_keys = fa 0000","Vimal"=>"250000","R

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

count() ) _or_countable [, int $mo $season=array("summer","wi


functi de = COUNT_NORMAL ] nter","spring","autumn");
on ) echo count($season);
count
s all ?>
eleme
nts in Output:
an
array. 4
5 PHP sort() PHP bool sort ( array &$arra <?php
sort()
functi y [, int $sort_flags = SO $season=array("summer","wi
on RT_REGULAR ] ) nter","spring","autumn");
sorts sort($season);
all the
eleme foreach( $season as $s )
nts in {
an echo "$s<br />";
array.
}
?>

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.

You might also like