Topic: PHP Basics – Part 4 of 10: Arrays in PHP (Indexed, Associative, Multidimensional)
---
1. What is an Array in PHP?
• An array is a special variable that can hold multiple values at once.
• In PHP, arrays can be indexed, associative, or multidimensional.
---
2. Indexed Arrays
• Stores values with a numeric index (starting from 0).
• Add elements:
• Count elements:
• Loop through indexed array:
---
3. Associative Arrays
• Uses named keys instead of numeric indexes.
• Loop through associative array:
---
4. Multidimensional Arrays
• Arrays containing one or more arrays.
• Loop through multidimensional array:
---
5. Array Functions You Should Know
•
•
•
•
•
•
•
•
---
6. Summary
• Arrays are powerful tools for storing multiple values.
• Indexed arrays use numeric keys; associative arrays use named keys.
• PHP supports nested arrays for more complex structures.
---
Exercise
• Create a multidimensional array of 3 students with their names and 2 grades.
• Print the average grade of each student using a nested loop.
---
#PHP #Arrays #Multidimensional #PHPTutorial #BackendDevelopment
https://t.iss.one/Ebooks2023
---
1. What is an Array in PHP?
• An array is a special variable that can hold multiple values at once.
• In PHP, arrays can be indexed, associative, or multidimensional.
---
2. Indexed Arrays
• Stores values with a numeric index (starting from 0).
$fruits = array("apple", "banana", "cherry");
echo $fruits[1]; // Output: banana• Add elements:
$fruits[] = "grape"; // Adds to the end of the array
• Count elements:
echo count($fruits); // Output: 4
• Loop through indexed array:
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}---
3. Associative Arrays
• Uses named keys instead of numeric indexes.
$person = array(
"name" => "Ali",
"age" => 30,
"city" => "Istanbul"
);
echo $person["name"]; // Output: Ali
• Loop through associative array:
foreach ($person as $key => $value) {
echo "$key: $value<br>";
}---
4. Multidimensional Arrays
• Arrays containing one or more arrays.
$students = array(
array("Ali", 90, 85),
array("Sara", 95, 88),
array("Omar", 78, 82)
);
echo $students[0][0]; // Output: Ali
echo $students[1][2]; // Output: 88
• Loop through multidimensional array:
for ($i = 0; $i < count($students); $i++) {
for ($j = 0; $j < count($students[$i]); $j++) {
echo $students[$i][$j] . " ";
}
echo "<br>";
}---
5. Array Functions You Should Know
•
count() – Number of elements•
array_push() – Add to end•
array_pop() – Remove last element•
array_merge() – Merge arrays•
in_array() – Check if value exists•
array_keys() – Get all keys•
sort(), rsort() – Sort indexed array•
asort(), ksort() – Sort associative array by value/key$colors = array("red", "blue", "green");
sort($colors);
print_r($colors);---
6. Summary
• Arrays are powerful tools for storing multiple values.
• Indexed arrays use numeric keys; associative arrays use named keys.
• PHP supports nested arrays for more complex structures.
---
Exercise
• Create a multidimensional array of 3 students with their names and 2 grades.
• Print the average grade of each student using a nested loop.
---
#PHP #Arrays #Multidimensional #PHPTutorial #BackendDevelopment
https://t.iss.one/Ebooks2023
❤3
Forwarded from Machine Learning with Python
Numpy @CodeProgrammer.pdf
2.4 MB
👨🏻💻 This is a long-term project to learn Python and NumPy from scratch. The main task is to handle numerical #data and #arrays in #Python using NumPy, and many other libraries are also used.
https://t.iss.one/CodeProgrammer
Please open Telegram to view this post
VIEW IN TELEGRAM
❤4