Arrays
Learning objectives :
11.5.2.1 use the technical terms associated with arrays including upper and lower bounds
11.5.2.3 write program code using 1D and 2D arrays
I. Definition
A data structure is a collection of elementary data types such as integer, real, boolean, and char.
An array is a data structure that contains finite set of indexed elements of the same type, united by the same name.
1D array is similar to a table of objects or primitive types, keyed by an index.
Examples,
1D Array: students
0
1
2
3
4
Diana
Abai
Malika
Zhanna
Ruslan
1D Array: marks
1D arrays in pseudocode:
students = [ "Diana ","Abai ","Malika ","Zhanna ","Ruslan "]
marks = [34 , 30 , 37 , 33 , 31 ]
OUTPUT marks[3] // output 33
OUTPUT students[0] // output Diana
OUTPUT students[1], " - ", marks[1] // output Abai - 30
II. Array processing
n - length of array
arr - array
Input elements of the array from the keyboard :
FOR i = 0 TO n - 1
INPUT arr[i]
endFOR
Output elements of the array :
FOR i = 0 TO n - 1
OUTPUT arr[i]
endFOR
Determining the index of the minimum element of the array and its value :
index = 0 // index of minimal element
min = arr[index] // value of minimal element
FOR i = 1 TO n - 1
IF min > arr[i]
THEN min = arr[i]
endFOR
OUTPUT index, min
Сalculating the sum of array elements :
sum = 0 // start sum
FOR i = 0 TO n - 1
sum = sum + arr[i]
endFOR
OUTPUT sum
Сounting the number of even elements of an array :
count = 0 // start count
FOR i = 0 TO n - 1
IF arr[i] MOD 2 = 0
THEN count = count + 1
endFOR
OUTPUT count
Outputting array elements with odd indices :
FOR i = 0 TO n - 1
IF i MOD 2 <> 0
THEN OUTPUT arr[i]
endFOR
III. PHP Indexed Array
1D arrays in PHP program:
<?php
$students = array ("Diana ","Abai ","Malika ","Zhanna ","Ruslan "); // array of five elements
$marks = array (34 , 30 , 37 , 33 , 31 );
echo $marks[3]."<br>" ; // output 33
echo $students[0]."<br>" ; // output Diana
echo $students[1]." - " .$marks[1]; // output Abai - 30
?>
Output all elements of an Indexed Array students.
<?php
$students = array ("Diana ","Abai ","Malika ","Zhanna ","Ruslan ");
$arrlength = count ($students); // count() - Counts the number of elements in an array
for ($x = 0 ; $x < $arrlength; $x++) // $arrlength = 5 (in array five elements)
{
echo $students[$x]."<br>" ;
}
?>
IV. PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
1D array result :
Diana
Abai
Malika
Zhanna
Ruslan
34
30
37
33
31
Example,
$result = array ("Diana "=>34 , "Abai "=>30 , "Malika "=>37 , "Zhanna "=>33 , "Ruslan "=>31 );
It means that
$result["Diana"] = 34
$result["Malika"] = 37
$result["Ruslan"] = 31
Output all elements of an Associative Array result.
<?php
$result = array ("Diana "=>34 , "Abai "=>30 , "Malika "=>37 , "Zhanna "=>33 , "Ruslan "=>31 );
foreach ($result as $x => $x_value) // iterate each element of array by key
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>" ;
}
?>
Display:
Key=Diana, Value=34
Key=Abai, Value=30
Key=Malika, Value=37
Key=Zhanna, Value=33
Key=Ruslan, Value=31
Task . Determine who got 34 or more marks.
<?php
$result = array ("Diana "=>34 , "Abai "=>30 , "Malika "=>37 , "Zhanna "=>33 , "Ruslan "=>31 );
echo "Students who got 34 or more marks:<br>" ;
foreach ($result as $x => $x_value)
{
if ($x_value >= 34 )
echo $x . "<br>" ;
}
?>
Some examples:
Convert string to array
$my_string="4 7 9 1 89";
$my_array = explode(" ", $my_string); //split a string into an array by spaces
$num = (int)($my_array[0]); // convert string to integer
Fill array of random numbers
$n = 10;
for($i=0; $i<$n; $i++){
$arr[$i] = rand(1, 100); // assign an element of array random number
}
Output elements of array in line
for($i=0; $i<$n; $i++){
print $arr[$i]." ";
}
Next line
print "<br>";
Questions :
What is an array, and how is it defined in programming?
Describe the process of inputting elements into an array from the keyboard.
Write a code snippet to calculate the sum of all elements in an array arr of length n .
How do you create an associative array?
Explain how to iterate over an associative array in PHP to display each key-value pair.
How to process associative arrays to print elements according to criteria?
Exercises :
Ex. 1 Define array elements
Ex. 2
Tasks on https://www.w3resource.com/php-exercises/php-array-exercises.php
Ex. 3 Implement the Luhn algorithm
Task on https://www.101computing.net/is-my-credit-card-valid/
Exam questions :