Useful functions for working with arrays in PHP

Long time since I wrote in bložek. Work and study does not give the rest. Recently, getting lots of orders for PHP, and often have to work with arrays of data. My knowledge of PHP is amazing, so constantly I climb into the documentation. Today got the idea to write a mini cheat sheet with useful functions for working with arrays in PHP. I think that this is not just me cheat sheet.
The most frequently used feature in my opinion is the count (“array). Count returns the number of elements in the array. Example of work:

 

In fact, the count function can work not only with arrays, but also with ordinary variables, as well as with multidimensional arrays. Here are a couple of examples:
If someone did not understand why the count ($myArr, COUNT_RECURSIVE) returns 8explain. An array of $myArr consists of 3 elements (two arrays and one number). The first element of the array is an array of $myArr of 3 elements. Means all elements have 6. We add here another two elements from the second array and get 8.
Go ahead. Another useful feature is in_array (value, “array, [match type]). In_array function searches for the element to seek value in the array. If it findsreturns TRUE, otherwise FALSE. The third argument is the need to match the type. If it is set to TRUE, it means that the value must match not only the value but also by type. In addition, the string comparison is case sensitive. Examples:

 

Moving on. Function array_search (value, “array, [match type]) is similar to the previous one, only in case of success returns the key value in the array. Examples:

Результат выполнения:

Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => A [1] => B [2] => C ).

Go ahead. Array sorting function. First look at sort (“array, [flags]). Sorts the array in ascending order, or alphabetical order if string. As additional flags you can set the following options: SORT_REGULAR (standard elements are compared), SORT_NUMERIC (numeric comparison elements), SORT_STRING (string comparison). More details can be found in the official documentation on this link. Examples of use:

Результат работы:

Array ( [0] => 10 [1] => 1 [2] => 12 [3] => 13 [4] => 2 [5] => 3 [6] => 11 [7] => 100 )
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 10 [4] => 11 [5] => 12 [6] => 13 [7] => 100 )
Array ( [0] => 1 [1] => 10 [2] => 100 [3] => 11 [4] => 12 [5] => 13 [6] => 2 [7] => 3 )

It’s simple, right? There is a similar feature, but sorts it in reverse orderrsort (array, [flags]). As well as two other functions that perform similar actions, but they retain the key values. The asort (array, [flags]) is a simple sort while maintaining keys and arsort (array, [flags]) is sorted in reverse order, with their keys. A similar example of work functions asort or arsort:

Результат работы немного другой:

Array ( [0] => 10 [1] => 1 [2] => 12 [3] => 13 [4] => 2 [5] => 3 [6] => 11 [7] => 100 )
Array ( [1] => 1 [4] => 2 [5] => 3 [0] => 10 [6] => 11 [2] => 12 [3] => 13 [7] => 100 )
Array ( [7] => 100 [3] => 13 [2] => 12 [6] => 11 [0] => 10 [5] => 3 [4] => 2 [1] => 1 )

Note that in this example, I used a flag SORT_STRING. In addition to these functions, there is a possibility to sort an array by keys. There are functions ksort (array, [flags]) and krsort (array, [flags]), which work in the same way, so we’ll do without an example🙂.
What if I want to sort an array by its way? This function can help me usort (“array, function) and uksort (“array, function) that sort of custom comparer members. That is, the programmer must write a function that will compare the elements of the array in turn, and yourself or have the uksort usort elements in the array, depending on the results of the comparison. An example is a bit more complicated. First I’ll explain the device array. It will consist of arrays that contain the string and number. For example, this:

 

Now change the items and sort it by the first element of the internal arrays. Code:

Результат работы программы:

Array ( [0] => Array ( [0] => 1 [1] => String1 ) [1] => Array ( [0] => 2 [1] => String2 ) [2] => Array ( [0] => 3 [1] => String3 ) )

As you can see there is nothing difficult. The array is sorted by the first element of the internal array. The comparison function may return a value of 3. If the positive means, the sort function finds the first element greater than the second, if negative means on the contrary and if “0, it means that the elements are equal. The function ursort also works, only sort in reverse order. By the way, you can sort in descending order, using usort, just setting the correct comparison function for the occasion.
Next awaits us array_walk (“array, function, [optional argument]). This function iterates through the array, passing the key and value pair in the user-defined function. An additional argument, if provided, will be transferred to the third in a user-defined function. If the error does not occur, returns TRUE, otherwiseFALSE. Let’s look at an example that would be clearer:

 

Результатом будет:

A is Ivan

B is John

C is Bob

Please note. That you could modify the elements of an array, you need to pass them by reference. For example, here in this way:

Результат работы:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Array ( [0] => 4 [1] => 5 )
Array ( [0] => 3 [1] => 4 [2] => 5 )

There is another similar function array_chunk (“array, “size, [Save keys]). This function returns a multidimensional array, consisting of the array with the specified size. If you specify a Boolean flag to keep the keys array values are obtained with the keys, which they had initially, if not all the values get ordinary digital keys. Example of work:

Результат работы:

Array ( [0] => Array ( [A] => 1 [B] => 2 ) [1] => Array ( [C] => 3 [D] => 4 ) [2] => Array ( [E] => 5 [F] => 7 ) [3] => Array ( [G] => 8 ) )

That’s all. Ask your answers, write reviews and comments. I hope the article will be useful to someone.