Arrays in PHP Programming

ASSOCIATIVE 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.
 
PROGRAM
:
 
<?php
 
echo "<br>" . "OUTPUT:" . "<br>";
 
$fruits = array('a'=>'apple','b'=>'banana','c'=>'pea');
 
echo "fruit[a] is " .$fruits['a'];
 
?>
 
OUTPUT: 
fruit[a] is apple
MODIFY ELEMENTS.
 
T
o
 
m
o
d
i
f
y
 
e
l
e
m
e
n
t
s
 
i
n
 
a
r
r
a
y
.
 
P
R
O
G
R
A
M
:
 
<
?
p
h
p
 
e
c
h
o
 
"
<
b
r
>
"
 
.
 
"
O
U
T
P
U
T
:
"
 
.
 
"
<
b
r
>
"
;
 
$
a
r
r
=
a
r
r
a
y
(
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
)
;
 
$
a
r
r
[
2
]
=
'
z
'
;
 
f
o
r
(
$
i
=
0
;
$
i
<
c
o
u
n
t
(
$
a
r
r
)
;
$
i
+
+
)
 
{
 
e
c
h
o
"
$
a
r
r
[
$
i
]
"
.
"
<
b
r
>
"
;
 
}
 
?
>
 
O
U
T
P
U
T
:
 
A  b    z   d
REMOVE ARRAY ELEMENTS
 
<?php
 
echo "<br>" . "OUTPUT:" . "<br>";
 
$arr=array('a','b','c','d');
 
unset($arr[2]);
 
for($i=0;$i<count($arr);$i++)
 
{
 
echo"$arr[$i]" . "<br>";
 
}
 
?>
 
OUTPUT:
 
A  
b
RETRIEVAL ARRAY SIZE.
 
 
<?php
 
$data=array('a','b','c');
 
echo "<br>" . "OUTPUT:" . "<br>";
 
echo "Array Size" . count($data) . "element";
 
?>
 
OUTPUT: Array Size3element
NESTED ARRAY.
 
<?php
 
echo "<br>" . "OUTPUT:" . "<br>";
 
$phonebook=array(
 
array('name'=>'ram','telno'=>'66665489878'),
 
array('name'=>'david','telno'=>'657665489878'));
 
print_r ( $phonebook[1]);
 
?>
 
OUTPUT:
 
Array ( [name] => david [telno] => 657665489878 )
PROCESS ARRAY WITH LOOP.
 
<?php
 
echo "<br>" . "OUTPUT:" . "<br>";
 
$city=array('kanpur','delhi','hydrabad');
 
for($i=0;$i<count($city);$i++)
 
print ($city[$i]."\n<br>");
 
print "<br>";
 
?>
 
 <?php
 
$city=array('kanpur','delhi','hydrabad');
 
foreach($city as $c)
 
print ($c."\n<br>");
 
print "<br>";
 
?>
 
<?php
 
$city=array('kanpur'=>'india','delhi'=>'india','hydrabad'=>'india');
 
foreach($city as $key=>$value)
 
print ("$value is in $key"."\n<br>");
 
print "<br>";
 
?>
 
OUTPUT:
 
kanpur
delhi
hydrabad
kanpur
delhi
hydrabad
india is in kanpur
india is in delhi
india is in hydrabad
FOREACH loop
 
This loop is used to iterate over arrays. For every counter of loop, an array
element is assigned and the next counter is shifted to the next element.
 
PROGRAM
:
 
<?php
 
echo "<br>" . "OUTPUT:" . "<br>";
 
$fruits = array('a'=>'apple','b'=>'banana','c'=>'pea');
 
foreach ($fruits as $value)
 
echo $value."<br>";
 
echo "<br>";
 
foreach ($fruits as $key => $value)
 
echo "$key: $value<br>";
 
?>
 
OUTPUT:
 
apple
banana
pea
a: apple
b: banana
c: pea
EXPLODE FUNCTION.
 
The explode function is used to "Split a string by a
specified string into pieces i.e. it breaks a string into
an array". The explode function in PHP allows us to
break a string into smaller text with each break
occurring at the same symbol. This symbol is known
as the delimiter.
 
Using the explode command we will create an array
from a string. The explode() function breaks a string
into an array.
 
PROGRAM
:
 
<?php
 
$arr="Twinker Tanker Tank";
 
$str=explode(" ",$arr);
 
echo "<br>" . "OUTPUT:" . "<br>";
 
print_r($str);
 
?>
 
OUTPUT: 
Array ( [0] => Twinker [1] => Tanker
[2] => Tank)
IMPLODE FUNCTION.
 
The implode function in PHP is used to "join
elements of an array with a string".The implode()
function returns a string from elements of an array.
It takes an array of strings and joins them together
into one string using a delimiter (string to be used
between the pieces) of your choice.  The implode
function in PHP is easily remembered as "array to
string", which simply means that it takes an array
and returns a string.It rejoins any array elements
and returns the resulting string, which may be put
in a variable.
 
PROGRAM
:
 
<?php
 
$arr=array('Twinker','Tanker','Tank');
 
$str=implode(" and ",$arr);
 
echo "<br>" . "OUTPUT:" . "<br>";
 
print("$str");
 
?>
 
OUTPUT: 
Twinker and Tanker and Tank.
Slide Note
Embed
Share

Arrays in PHP play a crucial role in storing and manipulating data efficiently. This snippet demonstrates the use of numerical and associative arrays, modifying elements, removing array elements, retrieving array size, handling nested arrays, and processing arrays with loops in PHP. Explore various array functionalities showcased in concise code examples.

  • PHP Programming
  • Arrays
  • Data Structure
  • Associative Array
  • Modifying Elements

Uploaded on Oct 09, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. ASSOCIATIVE 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. PROGRAM: <?php echo "<br>" . "OUTPUT:" . "<br>"; $fruits = array('a'=>'apple','b'=>'banana','c'=>'pea'); echo "fruit[a] is " .$fruits['a']; ?> OUTPUT: fruit[a] is apple

  2. MODIFY ELEMENTS. To modify elements in array. PROGRAM: <?php echo "<br>" . "OUTPUT:" . "<br>"; $arr=array('a','b','c','d'); $arr[2]='z'; for($i=0;$i<count($arr);$i++) { echo"$arr[$i]"."<br>"; } ?> OUTPUT: A b z d

  3. REMOVE ARRAY ELEMENTS <?php echo "<br>" . "OUTPUT:" . "<br>"; $arr=array('a','b','c','d'); unset($arr[2]); for($i=0;$i<count($arr);$i++) { echo"$arr[$i]" . "<br>"; } ?> OUTPUT: A b

  4. RETRIEVAL ARRAY SIZE. <?php $data=array('a','b','c'); echo "<br>" . "OUTPUT:" . "<br>"; echo "Array Size" . count($data) . "element"; ?> OUTPUT: Array Size3element

  5. NESTED ARRAY. <?php echo "<br>" . "OUTPUT:" . "<br>"; $phonebook=array( array('name'=>'ram','telno'=>'66665489878'), array('name'=>'david','telno'=>'657665489878')); print_r ( $phonebook[1]); ?> OUTPUT: Array ( [name] => david [telno] => 657665489878 )

  6. PROCESS ARRAY WITH LOOP. <?php <?php $city=array('kanpur'=>'india','delhi'=>'india','hydrabad'=>'india'); echo "<br>" . "OUTPUT:" . "<br>"; foreach($city as $key=>$value) $city=array('kanpur','delhi','hydrabad'); print ("$value is in $key"."\n<br>"); for($i=0;$i<count($city);$i++) print "<br>"; print ($city[$i]."\n<br>"); ?> print "<br>"; OUTPUT: ?> kanpur delhi hydrabad <?php $city=array('kanpur','delhi','hydrabad'); kanpur delhi hydrabad foreach($city as $c) print ($c."\n<br>"); india is in kanpur india is in delhi india is in hydrabad print "<br>"; ?>

  7. FOREACH loop This loop is used to iterate over arrays. For every counter of loop, an array element is assigned and the next counter is shifted to the next element. PROGRAM: OUTPUT: <?php apple banana pea echo "<br>" . "OUTPUT:" . "<br>"; $fruits = array('a'=>'apple','b'=>'banana','c'=>'pea'); foreach ($fruits as $value) a: apple b: banana c: pea echo $value."<br>"; echo "<br>"; foreach ($fruits as $key => $value) echo "$key: $value<br>"; ?>

  8. EXPLODE FUNCTION. The explode function is used to "Split a string by a specified string into pieces i.e. it breaks a string into an array". The explode function in PHP allows us to break a string into smaller text with each break occurring at the same symbol. This symbol is known as the delimiter. PROGRAM: <?php $arr="Twinker Tanker Tank"; $str=explode(" ",$arr); echo "<br>" . "OUTPUT:" . "<br>"; Using the explode command we will create an array from a string. The explode() function breaks a string into an array. print_r($str); ?> OUTPUT: Array ( [0] => Twinker [1] => Tanker [2] => Tank)

  9. IMPLODE FUNCTION. The implode function in PHP is used to "join elements of an array with a string".The implode() function returns a string from elements of an array. It takes an array of strings and joins them together into one string using a delimiter (string to be used between the pieces) of your choice. The implode function in PHP is easily remembered as "array to string", which simply means that it takes an array and returns a string.It rejoins any array elements and returns the resulting string, which may be put in a variable. PROGRAM: <?php $arr=array('Twinker','Tanker','Tank'); $str=implode(" and ",$arr); echo "<br>" . "OUTPUT:" . "<br>"; print("$str"); ?> OUTPUT: Twinker and Tanker and Tank.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#