An array stores multiple values in one single variable:
// Create an array
$numList = array(10,13,15,16,14,18,19,17);
$numList2 = [12,15,14,61,84,58,7];
print_r($numList);
echo "<br>";
echo "<br>";
// Create an associtaive array
$nameList = ['firstName' => 'Anil', 'lastName' => 'Singh'];
print_r($nameList);
echo "<br>";
// Sort array using inbuilt function
sort($numList);
print_r($numList);
echo "<br>";
// Reverse Sort array using rsort function
rsort($numList);
print_r($numList);
echo "<br>";
// Getting maximum value from an aaray
echo max($numList);
echo "<br>";
// Getting minimum value from array
echo min($numList);
$emp = [
[1, "Rahul", "Manager", 50000],
[1, "Manish", "Engineer", 35000],
[1, "Ashok", "Assistant", 15000],
[1, "Tanmay", "Sales Boy", 5000],
];
// For Loop
for ($row = 0; $row < count($emp); $row++) {
for ($col = 0; $col < count($emp[$row]); $col++) {
echo $emp[$row][$col] . " ";
}
echo "<br>";
}
echo "<br>";
echo "<br>";
// echo "<pre>";
// print_r($emp);
// echo "<pre>";
echo "<br>";
echo "<br>";
echo "<table border='2px' cellpadding='5px' cellspacing='0'>";
foreach ($emp as $value1) {
echo "<tr>";
foreach ($value1 as $value2) {
echo "$value2 ";
}
echo "</tr>";
}
echo "</table>";
$num_list = [10, 15, 45, 32, 54, 99, 88, 45, 62];
echo "<pre>";
print_r($num_list);
echo "</pre>";
echo "<br>";
for ($counter = 0; $counter < count($num_list); $counter++) {
echo $num_list[$counter] . "<br>";
}
echo "<br>";
sort($num_list);
for ($counter = 0; $counter < count($num_list); $counter++) {
echo $num_list[$counter] . "<br>";
}
echo "<br>";
rsort($num_list);
for ($counter = 0; $counter < count($num_list); $counter++) {
echo $num_list[$counter] . "<br>";
}
$cars = [
["BMW", 25, 15],
["Volvo", 30, 12],
["Saab", 40, 10],
["Land Rover", 46, 24]
];
echo "<pre>";
print_r($cars);
echo "</pre>";
echo "<br>";
for ($i = 0; $i < 4; $i++) {
echo "<p><b>Car Number " . $i + 1 . " :</b></p>";
echo "<ul>";
for ($j = 0; $j < 3; $j++) {
echo "<li>" . $cars[$i][$j] . "</li>";
}
echo "</ul>";
}
0 comments
Post a Comment