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.
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 08.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes