https://github.com/php/php-src
I will learn the array methods part 04 ( array_chunk , array_combine , array_count_values and array_product ).
In this tutorial we will continue our series about the PHP programming language, we will talk about the arrays methods the fourth part (Array_Chunk, Array_Combine, Array_Count_Values and Array_Product ), let's start ..
The array_chunk method divides the array into smaller arrays each with a number of elements equal to the $size parameter value. The last small array may contain fewer elements than the $size parameter value.
It has 3 parameters the first is the " Array " , the second is " Size " which is the size of each partial array is cut from the original array and the third parameter is the " Perverse Key " by default the value is false it means the indexes in the new array or in the small array will start from 0 , if you change this value to true, the array will keep the keys in the resulting small arrays. This method returns a multidimensional array numerically, starting at zero.
Each dimension of the array contains a number of elements equal to the value of the $ size parameter. This method causes an error of type E_WARNING if the $size operand is smaller than 1 and the method will return NULL in that case.
There is two ways to use the array_chunk , this method will return a multidimensional array so you can create an array and get the result in this array , the second way is to print directly the array using " print_r " method
$result = array_chunk($array);
print_r(array_chunk($array));
I have an array and it contains 5 persons , I want to divide this array into small arrays each one will contain 2 persons and to do that we need to use the array_chunk method
$teamI = array( "Persone1", "Persone2","Persone3","Persone4","Persone5");
echo "<pre>";
print_r($teamI);
$groupI = array_chunk($teamI, 2);
print_r($groupI);
This is my code I will firstly print the $teamI array then I will print the result $groupI
If you look at the indexes , for each small array the keys starting from 0 , I want to keep the original keys for each value and to do that we need to change the third parameter to true and this is the result
The method creates a new array by using the values of the array passed through the keys parameter as keys, and using the values of the array passed through the parameter values as values associated with these keys.
It has two parameters , the first is the keys which is an array of keys that this function uses. Any value that is not accepted as a key will be converted to a text string and the second parameter is the values which is another array contains the values that you will use.
The method returns a new array resulting from merging the key and value arrays, and returns FALSE if the number of elements in the two arrays is uneven.
The method causes an E_WARNING error if the number of elements in the value and key arrays is not equal.
To use the array_combine we have two ways , the normal way to create an array and the second way to print it directly
$result = array_combine($arrayKeys, $arrayValues);
print_r(array_combine($arrayKeys, $arrayValues));
I will create an associative array using the array_combine method , I will create two indexed arrays for the keys and values , this is the code
$languagesKeys = array("en","fr","es");
$languagesValues = array("English","French","Spanish");
$languages = array_combine($languagesKeys, $languagesValues);
print_r($languages);
The array_combine will combine between the keys and values and this is the result
The array_count_values function creates a new array that contains the values in the array passed by the array as keys, and the frequency of each value in that array as values associated with those keys.
The method returns a new array of values in the passed array, and each value is associated with how many times it is repeated in the array.
It will generate an E_WARNING error for each element in the array, not a string or an integer.
To use the array_count_values we pass the array as parameter
$result = array_count_values($array);
print_r(array_count_values($array));
I will count the number of each person in the array and for that I will use this method
$votes = array("persone1","persone2","persone1","persone3","persone5","persone3","persone2","persone1","persone2","persone2","persone3","persone4","persone5","persone1");
print_r($votes);
print_r(array_count_values($votes));
The method will count and return for as an array the keys are the persons and the values are the numbers
The array_product () method returns the value of multiplying the values of the array elements.
It has the array as parameter , and it twill return the product as an integer or float.
To use the array_product method we have two ways, the first is to pass an array as parameter , and the second to pass values
$result = array_product($array);
$result = array_product($value1,$value2,$value3);
I will create an array of numbers , I will use the array_product and this is the code
$number = array(2,4,3);
echo("The result is : " . array_product($number));
The array contains 3 values " 2, 4 , 3 " , the array product will multiply 2 by 4 and the result by 3
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/chank_combine.php