https://github.com/php/php-src
I will learn the third part of array methods ( Array_Sum, Array_Rand, Array_Column and Array_Unique) with examples.
In this tutorial we will continue our series about the PHP programming language, we will talk about the arrays methods the third part ( Array_Sum, Array_Rand, Array_Column and Array_Unique ), let's start ..
The array_sum method calculates the sum of array element values. It has only one parameter which is the array , it returns the sum of array element values as an integer or float; or 0 if the array is empty.
To use this method you must have an array contains only numbers
array_sum($array);
It will return the sum of elements, in my example I have created a table of languages and the result of each language
I will calculate the sum of all these results using the array_sum method and this is the code :
<?php
echo "The Sum is " . array_sum($results) . "<br>";
echo "The Average is : " . array_sum($results) / $numb . "%";
?>
This code will return the sum of the results and also it will calculate the average , this is the result
The array_rand method randomly selects one or more array elements, and returns the element key (s) randomly. A false random number generator is used for encryption purposes.
It has 2 parameters the first is the array and the second is the num which is the number of elements that you want to select randomly. When you select only one entry, the array_rand function returns a key for one item at random. Otherwise, the array will return random item keys, By selecting random element keys from the array as well as the values of these elements.
Trying to select more items from the array will cause an E_WARNING error, and the value NULL will be returned.
To use the array_rand method we need to pass the array and the num as parameters
$randomArray = arra_rand($array, num);
I have to clubs " ClubA and ClubB " each one has 4 clubs I will use two groups " GroupA and GroupB " in each group I will select 3 clubs randomly and to do that we need to use the array_rand method
$clubA = array("club1","club2", "club3","club4");
$clubB = array("club5","club6", "club7","club8");
$groupA = array_rand($clubA, 3);
$groupB = array_rand($clubB, 3);
GroupA and GroupB now contains keys , and to print the clubs we need to use the array[key]
<?php
foreach ($groupA as $groupa) {
echo "<li>" . $clubA[$groupa] . "</li>";
}
?>
<?php
foreach ($groupB as $groupb) {
echo "<li>" . $clubB[$groupb] . "</li>";
}
?>
I have used the foreach, I will print by the echo method the elements of the ClubA and ClubB using the keys in groupA and groupB and this is the result
If I refrech the page I will get other clubs because the array_rand will select the elements radnomly
The array_column method returns the values from one column in the " $input " array, these values are registered with the key specified in the " $column_key " parameter. Optionally, another column can be specified for the method to be used as a key for the array returned by the " $index_key " parameter.
The Input:
is the array that we pass as parameter.
Column_Key:
The values column will return the method. This value can be an integer representing the column key that the method returns, or a text key in the case of associative arrays, or the name of an object property. If the value of this parameter is equal to NULL, the methid returns the array or the objects are complete .
Index_key :
This parameter specifies the column that will be used as the key for the array that the method will return. The index key value may be an integer representing the column key, or a text key.
Returned values :
This method returns an array of values representing one column of the array to be processed.
To use the column key we must have an array multidimensional and this is the syntax
$arrayC = array_column($input, $column_key, $index_key);
I have an array of teachers , each teacher has the name and specialization, I want to get the names of all teachers that I have and to do that I need to use the array_column method
$teachers = array(
array(
"Name" => "Alexendre",
"Specialization" => "Medecin"
),
array(
"Name" => "Arden",
"Specialization" => "Engennering"
),
array(
"Name" => "Mick",
"Specialization" => "Mathematics"
)
);
$teachersC = array_column($teachers, "Name", "Specialization");
It will get the names of all teachers and use the specialization as keys and this is the result
The array_unique method deletes duplicate values from the inserted array and returns a new array without duplicate values. The keys are saved , if the comparison of elements is equal ,it will save the key and the value of the first element.
We can say for two elements that they are equals if the first element is identical to the second element, It has the array as parameter and it returns another array.
To use the array_unique method we must pass the array as parameter
$arrayUnique = array_unique($array);
I will use an example of participants each one can send more than message of participant then I will take the real number of participants
$participants = array( "Alexendre", "Alex","Alexendre","Arden","Alex","Mick","John","John", "Mick","Alexendre","Alex","John","Arden");
$participantsUnique = array_unique($participants);
I will print the elements of the participantsUnique and this is the result
This is the normal array and after I used the array_unique method, this is the result
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/array_methods4.php