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

3.PHP Arrays

PHP arrays allow storing of multiple elements of similar data types. There are three main types of PHP arrays: indexed arrays, associative arrays, and multidimensional arrays. Indexed arrays use numeric indexes to store elements, associative arrays use named keys, and multidimensional arrays store arrays within arrays. PHP also provides sorting functions like sort(), asort(), and ksort() to sort array elements by value or key.

Uploaded by

kashish7377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

3.PHP Arrays

PHP arrays allow storing of multiple elements of similar data types. There are three main types of PHP arrays: indexed arrays, associative arrays, and multidimensional arrays. Indexed arrays use numeric indexes to store elements, associative arrays use named keys, and multidimensional arrays store arrays within arrays. PHP also provides sorting functions like sort(), asort(), and ksort() to sort array elements by value or key.

Uploaded by

kashish7377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PHP

ARRAYS
PHP ARRAYS
type of data structure that allows us to store multiple
elements of similar data types
PHP-INDEXED ARRAYS
PHP-ASSOCIATIVE ARRAYS

$name[“key1"]=“1";
$name[“key2"]=“2";
$name[“key3"]=“3";
PHP –INDEXED ARRAYS
<?php
$WT = array("HTML", "CSS", "JavaScript”,
PHP");
$length= count($WT);
for($x = 0; $x < $length; $x++) {
echo $WT[$x];
echo "<br>";
}
?>
PHP – ASSOCIATIVE ARRAYS
<?php
$wt = array("HTML"=>"FONTEND",
"CSS"=>"FRONTEND", "PHP"=>"BACKEND");
foreach($wt as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
PHP – MULTI-DIMENSIONAL ARRAYS
Example 1(Indexed):

<?php
$wt = array (
array("HTML","frontend",1),
array("CSS", "frontend",2),
array("PHP", "backend",3));
PHP – MULTI-DIMENSIONAL ARRAYS
Example 1(contd..):

for ($row = 0; $row < count($wt); $row++) {


echo "<p><b>Row number $row</b></p>";

for ($col = 0; $col < 3; $col++) {


echo "<li>".$wt[$row][$col]."</li>";
}
}
?>
PHP – MULTI-DIMENSIONAL ARRAYS
Example 2:

<?php
$details = array(
array("name" => "aaa",
"mob" => "5645741523",
"email" => "[email protected]"),
array("name" => "bbb",
"mob" => "2584369721",
"email" => "[email protected]"));
PHP – MULTI-DIMENSIONAL ARRAYS
Example 2(contd…):

$keys = array_keys($details);
for($i = 0; $i < count($details); $i++) {
echo $keys[$i] . "\n";
foreach($details[$keys[$i]] as $key => $value) {
echo $key . " : " . $value ;
echo "<br>";
}
echo "<br";
}
?>
PHP – MULTI-DIMENSIONAL ARRAYS
Example 3:

<?php
$details = array(
"person1" => array(
"name"=>"aaa",
"mob" => "5689741523",
"email" => "[email protected]"),
"person2" => array(
"name"=>"bbb",
"mob" => "2584369721",
"email" => "[email protected]")
);
PHP – MULTI-DIMENSIONAL ARRAYS
Example 3:

$keys = array_keys($details);
for($i = 0; $i < count($details); $i++) {
echo $keys[$i] . "\n";
foreach($details[$keys[$i]] as $key => $value) {
echo $key . " : " . $value ;
echo "<br>";
}
echo "<br";
}
?>
PHP – SORT FUNCTIONS in ARRAYS

•sort() - sort arrays in ascending order


•rsort() - sort arrays in descending order
•asort() - sort associative arrays in ascending order, according to the value
•ksort() - sort associative arrays in ascending order, according to the key
•arsort() - sort associative arrays in descending order, according to the value
•krsort() - sort associative arrays in descending order, according to the key
PHP – SORT FUNCTIONS in ARRAYS

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
PHP – SORT FUNCTIONS in ARRAYS

<?php
$age = array(“bbb"=>"35", “aaa"=>"37",
“ccc"=>"43");
asort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" .
$x_value;
echo "<br>";
}
?>
PHP – SORT FUNCTIONS in ARRAYS

<?php
$age = array(“bbb"=>"35", “aaa"=>"37",
“ccc"=>"43");
ksort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" .
$x_value;
echo "<br>";
}
?>
PHP – ARRAY FUNCTIONS

https://www.w3schools.com/php/php_ref_array.asp

You might also like