PHP Tutorial #04 Array Methods (Array_Sum, Array_Rand, Array_Column and Array_Unique)

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

I will learn the third part of array methods ( Array_Sum, Array_Rand, Array_Column and Array_Unique) with examples.

  •  How to calculate the sum of elements using the array_sum method.
  • How to select elements randomly using array_rand method.
  • What's the array_column and how to use it.
  • How to delete the duplicates values using array_unique method.

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example) 

Difficulty

  • Basic

Description 

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 ..

1- Array_Sum Method :

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

2- Array_Rand Method :

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

3- Array_Column Method :

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 

4- Array_Unique Method :

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

Video Tutorial

Sort:  

Hi @akexebdre-maxim

I just reviewed your #3 and this is #4 of php tutorial, so you would not have time to implement my suggestion in the last review.

Since this is basic tutorial, it is good to go into deeper teaching and make your tutorial unique. If this can be found easily on the internet, you need to ask yourself: what makes your tutorial different so that people will follow your tutorial lessons and learn from you.

With basic concepts, always make your tutorial unique and extraordinary.

Learn how to put in deeper principles behind the concepts that you teach.

I look forward to your next contribution.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @rosatravels!

So far this week you've reviewed 2 contributions. Keep up the good work!

Thank you @rosatravels , I will try to be different .

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Thank you for your support

Coin Marketplace

STEEM 0.35
TRX 0.12
JST 0.040
BTC 70797.92
ETH 3553.00
USDT 1.00
SBD 4.76