Basic functions for working with arrays in PHP, the array prefix
You have to work with arrays in any programming language; PHP has a number of special functions for conveniently working with arrays. Arrays allow you to store a large amount of information; they are complex structures that can contain various data.
There are several kinds of arrays in PHP. Arrays can be simple, they can be associative , in which there are key-value matches. Arrays can also be nested , one can easily nest another.
Most array functions begin with array_ to quickly apply the desired function. For example, the IDE or some editor may support auto-completion, in which case the developer just needs to write array_ and then all functions for working with arrays will be offered. But there are also functions whose names you need to remember or look at the reference book. Such functions begin without the array_ prefix .
Let's see what are the main functions in PHP for working with arrays :
- array_flip - swaps keys with their values in an array
- array_intersect - calculates the convergence of arrays,
- array_keys - returns all or some subset of array keys,
- array_merge - merge one or more arrays,
- array_pop - retrieves the last element of the array,
- array_push - adds one or more elements to the end of the array,
- array_rand - selects one or more random keys from an array,
- array_reverse - returns an array with elements in reverse order,
- array_search - searches for the given value in the array and returns the key of the first found element if successful,
- array_shift - extracts the first element of the array,
- array_slice - selects an array slice,
- array - creates an array,
- asort - sorts the array, keeping the keys,
- count - counts the number of elements in an array or something in an object,
- current - returns the current element of the array,
- end - sets the internal array pointer to its last element,
- in_array - checks if a value is present in the array
- key - selects a key from an array
- reset - sets the internal array pointer to its first element.
Thus, working with arrays in PHP is quite simple, just memorize a few simple functions and apply them to achieve the desired result.
- IT / Misc 08.07.21 How to make a free translation for a website without an API, translate documents in Google Translate
- IT / Misc 06.07.21 How to make a subscription button on a website, a subscriber base and automatic mailing
- Food / Misc 06.07.21 How to quickly cook delicious fried pies with potatoes and onions
- IT / Misc 04.07.21 Caching - create, load and reset. Where to store the cache, methods and types of caching
- IT / Database 03.07.21 Custom NoSQL - storing data in files and not only in a database. Storing settings, small data and caching files