In this tutorial, I am going to make a list of common PHP array functions, with examples of usage and best practices. Every PHP developer must know how to use them and how to combine array functions to make code readable and short. Show Also, there is a presentation with given code examples, so you can download it from the related links and show it to your colleagues to build a stronger team. The BasicsThere are two different ways of creating arrays. One is to use /* Array
36 to specify the elements as key-value pairs. The other method is to put all elements inside /* Array
37. There are two important points that you should remember when creating associate arrays with key pairs.First, the key always has to be unique. If you try to use the same key multiple times in an array, PHP will ignore all other key-value pairs except the last one. Second, if a key is created as floats, bools, and valid string representations of integers, then it will be cast to integers. Here are a few examples of creating arrays in PHP: 1 $first = array(10, "Apple", 20, -18, "Monkey"); 2 print_r($first); 3 /* Array
4 (
5 [0] => 10
$first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");1 $first = array(10, "Apple", 20, -18, "Monkey");2 $first = array(10, "Apple", 20, -18, "Monkey");3 $first = array(10, "Apple", 20, -18, "Monkey");4 $first = array(10, "Apple", 20, -18, "Monkey");5 $first = array(10, "Apple", 20, -18, "Monkey");6 $first = array(10, "Apple", 20, -18, "Monkey");7 $first = array(10, "Apple", 20, -18, "Monkey");8 $first = array(10, "Apple", 20, -18, "Monkey");9 20 21 22 23 24 25 /* Array
27 (
29 [0] => 10
print_r($first);1 $first = array(10, "Apple", 20, -18, "Monkey");1 print_r($first);3 $first = array(10, "Apple", 20, -18, "Monkey");3 print_r($first);5 $first = array(10, "Apple", 20, -18, "Monkey");5 print_r($first);7 $first = array(10, "Apple", 20, -18, "Monkey");7 print_r($first);9 $first = array(10, "Apple", 20, -18, "Monkey");9 31 32 33 34 35 36 /* Array
38 (
/* Array
0 [0] => 10
/* Array
2/* Array
3/* Array
4$first = array(10, "Apple", 20, -18, "Monkey");3 /* Array
6/* Array
7/* Array
8/* Array
940 $first = array(10, "Apple", 20, -18, "Monkey");9 As you can see, using either /* Array
36 or /* Array
37 is equivalent when creating arrays. The shorthand notation has been available starting from PHP 5.4.You also don't need to specify a key for every array value. When left out, PHP sets the key to one more than the largest specified integer key. All automatically assigned keys will be greater than or equal to 0. Working With Keys and ValuesLet's start with the basic functions that work with array keys and values. One of them is /* Array
40, which creates an array using one array for keys and another for its values:1 43 2 45 3 4 48 5 (
0$first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 (
3$first = array(10, "Apple", 20, -18, "Monkey");4 (
5$first = array(10, "Apple", 20, -18, "Monkey");6 (
7$first = array(10, "Apple", 20, -18, "Monkey");8 (
920 51 21 53 You should know that the /* Array
41 function returns an indexed array of values, /* Array
42 returns an array of keys of a given array, and /* Array
43 exchanges keys with values:1 55 2 57 3 59 4 5 (
3$first = array(10, "Apple", 20, -18, "Monkey");0 (
5$first = array(10, "Apple", 20, -18, "Monkey");2 [0] => 10
6$first = array(10, "Apple", 20, -18, "Monkey");4 [0] => 10
8$first = array(10, "Apple", 20, -18, "Monkey");6 51 $first = array(10, "Apple", 20, -18, "Monkey");8 $first = array(10, "Apple", 20, -18, "Monkey");02 You can check if an array contains a specific value and get its first corresponding key using the /* Array
44 function. You can also use /* Array
45 if you just want to know whether an array contains a specific element and are not interested in its position. Consider using the /* Array
46 function when you want to check if the array uses a given key.1 $first = array(10, "Apple", 20, -18, "Monkey");04 2 3 $first = array(10, "Apple", 20, -18, "Monkey");07 4 $first = array(10, "Apple", 20, -18, "Monkey");09 5 $first = array(10, "Apple", 20, -18, "Monkey");11 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");13 $first = array(10, "Apple", 20, -18, "Monkey");2 $first = array(10, "Apple", 20, -18, "Monkey");4 $first = array(10, "Apple", 20, -18, "Monkey");16 $first = array(10, "Apple", 20, -18, "Monkey");6 $first = array(10, "Apple", 20, -18, "Monkey");18 $first = array(10, "Apple", 20, -18, "Monkey");8 $first = array(10, "Apple", 20, -18, "Monkey");11 20 $first = array(10, "Apple", 20, -18, "Monkey");22 21 23 $first = array(10, "Apple", 20, -18, "Monkey");25 25 27 $first = array(10, "Apple", 20, -18, "Monkey");28 29 $first = array(10, "Apple", 20, -18, "Monkey");30 print_r($first);1 print_r($first);3 $first = array(10, "Apple", 20, -18, "Monkey");33 print_r($first);5 $first = array(10, "Apple", 20, -18, "Monkey");35 print_r($first);7 print_r($first);9 $first = array(10, "Apple", 20, -18, "Monkey");38 31 32 $first = array(10, "Apple", 20, -18, "Monkey");41 34 $first = array(10, "Apple", 20, -18, "Monkey");43 36 $first = array(10, "Apple", 20, -18, "Monkey");11 38 $first = array(10, "Apple", 20, -18, "Monkey");47 As the example above shows, make sure you use strict type checking if you don't want any unexpected results. If you want to look up multiple elements in an array, it's usually faster to check if it contains a particular value by first flipping the array with /* Array
43 and then using /* Array
46.Make Your Code ShorterThe /* Array
49 function, which is not really a function but a language construction, is designed to assign variables in a short way. For example, here's a basic example of using the /* Array
49 function:1 $first = array(10, "Apple", 20, -18, "Monkey");49 2 $first = array(10, "Apple", 20, -18, "Monkey");51 3 4 $first = array(10, "Apple", 20, -18, "Monkey");54 5 $first = array(10, "Apple", 20, -18, "Monkey");56 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");58 $first = array(10, "Apple", 20, -18, "Monkey");2 $first = array(10, "Apple", 20, -18, "Monkey");60 $first = array(10, "Apple", 20, -18, "Monkey");4 $first = array(10, "Apple", 20, -18, "Monkey");6 $first = array(10, "Apple", 20, -18, "Monkey");63 $first = array(10, "Apple", 20, -18, "Monkey");8 $first = array(10, "Apple", 20, -18, "Monkey");65 This construction works perfectly with functions like /* Array
51 or /* Array
52 . Also, you can skip some parameters if you don't need them to be defined:1 $first = array(10, "Apple", 20, -18, "Monkey");67 2 $first = array(10, "Apple", 20, -18, "Monkey");69 3 $first = array(10, "Apple", 20, -18, "Monkey");71 Also, /* Array
49 can be used with /* Array
54, which makes this construction even better:1 $first = array(10, "Apple", 20, -18, "Monkey");73 2 3 $first = array(10, "Apple", 20, -18, "Monkey");76 4 $first = array(10, "Apple", 20, -18, "Monkey");78 5 $first = array(10, "Apple", 20, -18, "Monkey");80 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");11 With the /* Array
55 function, you can export an associative array to variables. For every element of an array, a variable will be created with the name of a key and value as a value of the element:1 $first = array(10, "Apple", 20, -18, "Monkey");84 2 $first = array(10, "Apple", 20, -18, "Monkey");86 3 $first = array(10, "Apple", 20, -18, "Monkey");88 4 $first = array(10, "Apple", 20, -18, "Monkey");90 5 $first = array(10, "Apple", 20, -18, "Monkey");92 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 $first = array(10, "Apple", 20, -18, "Monkey");95 $first = array(10, "Apple", 20, -18, "Monkey");4 $first = array(10, "Apple", 20, -18, "Monkey");6 $first = array(10, "Apple", 20, -18, "Monkey");98 Be aware that /* Array
55 is not safe if you are working with user data (like results of requests), so it's better to use this function with the flags /* Array
57 and /* Array
58.The opposite of the previous function is the /* Array
59 function, which makes an associative array from variables:1 200 2 202 3 204 4 5 207 $first = array(10, "Apple", 20, -18, "Monkey");0 (
0$first = array(10, "Apple", 20, -18, "Monkey");2 $first = array(10, "Apple", 20, -18, "Monkey");4 (
3$first = array(10, "Apple", 20, -18, "Monkey");6 (
5$first = array(10, "Apple", 20, -18, "Monkey");8 216 20 218 21 220 23 $first = array(10, "Apple", 20, -18, "Monkey");02 Filtering FunctionsThere is a great function for array filtering, and it is called /* Array
60. Pass the array as the first param and an anonymous function as the second param. Return /* Array
61 in a callback function if you want to leave this element in the array, and /* Array
62 if you don't:1 224 2 3 227 4 229 5 231 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 234 There is a way to filter not only by the values. You can use /* Array
63 or /* Array
64 as a third parameter to pass the key or both value and key to the callback function.Also, you can call /* Array
60 without a callback to remove all empty values:1 236 2 3 239 4 5 242 You can get only unique values from an array using the /* Array
66 function. Notice that the function will preserve the keys of the first unique elements:1 244 2 246 3 4 249 5 $first = array(10, "Apple", 20, -18, "Monkey");0 (
3$first = array(10, "Apple", 20, -18, "Monkey");2 (
5$first = array(10, "Apple", 20, -18, "Monkey");4 256 $first = array(10, "Apple", 20, -18, "Monkey");6 258 $first = array(10, "Apple", 20, -18, "Monkey");8 260 20 262 21 264 23 53 With /* Array
67, you can get a list of column values from a multi-dimensional array, like an answer from a SQL database or an import from a CSV file. Just pass an array and column name:1 $first = array(10, "Apple", 20, -18, "Monkey");84 2 270 3 272 4 274 5 $first = array(10, "Apple", 20, -18, "Monkey");92 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 279 $first = array(10, "Apple", 20, -18, "Monkey");4 $first = array(10, "Apple", 20, -18, "Monkey");6 282 Starting from PHP 7, /* Array
67 becomes even more powerful, because it is now allowed to . So working with an array of models just became easier:1 284 2 286 Walking Through the ArraysUsing /* Array
69, you can apply a callback to every element of an array. You can pass a function name or anonymous function to get a new array based on the given array:1 288 2 290 3 4 293 5 $first = array(10, "Apple", 20, -18, "Monkey");0 296 $first = array(10, "Apple", 20, -18, "Monkey");2 298 $first = array(10, "Apple", 20, -18, "Monkey");4 print_r($first);00 $first = array(10, "Apple", 20, -18, "Monkey");6 print_r($first);02 $first = array(10, "Apple", 20, -18, "Monkey");8 20 print_r($first);05 There is a myth that there is no way to pass values and keys of an array to a callback, but we can bust it: 1 print_r($first);07 2 3 print_r($first);10 4 print_r($first);12 5 print_r($first);14 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 print_r($first);17 $first = array(10, "Apple", 20, -18, "Monkey");4 print_r($first);19 $first = array(10, "Apple", 20, -18, "Monkey");6 $first = array(10, "Apple", 20, -18, "Monkey");8 (
320 (
521 print_r($first);26 23 print_r($first);28 25 $first = array(10, "Apple", 20, -18, "Monkey");02 But this looks dirty. It is better to use /* Array
70 instead. This function looks the same as /* Array
69, but it works differently. First of all, an array is passed using a reference, so /* Array
70 doesn't create a new array, but changes a given array. So as a source array, you can pass the array value using a reference in a callback. Array keys can also be passed easily:1 print_r($first);32 2 print_r($first);34 3 print_r($first);36 4 print_r($first);38 5 $first = array(10, "Apple", 20, -18, "Monkey");92 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 print_r($first);43 $first = array(10, "Apple", 20, -18, "Monkey");4 print_r($first);45 $first = array(10, "Apple", 20, -18, "Monkey");6 231 $first = array(10, "Apple", 20, -18, "Monkey");8 20 print_r($first);50 21 23 (
325 (
527 print_r($first);57 29 print_r($first);59 print_r($first);1 print_r($first);61 print_r($first);3 $first = array(10, "Apple", 20, -18, "Monkey");02 Joining the ArraysThe best way to merge two or more arrays in PHP is to use the /* Array
73 function. Items of arrays will be merged together, and values with the same string keys will be overwritten with the last value:1 print_r($first);65 2 print_r($first);67 3 4 print_r($first);70 5 print_r($first);72 $first = array(10, "Apple", 20, -18, "Monkey");0 (
3$first = array(10, "Apple", 20, -18, "Monkey");2 (
5$first = array(10, "Apple", 20, -18, "Monkey");4 print_r($first);78 $first = array(10, "Apple", 20, -18, "Monkey");6 print_r($first);80 $first = array(10, "Apple", 20, -18, "Monkey");8 print_r($first);82 20 print_r($first);84 21 53 To remove array values from another array (or arrays), use /* Array
74. To get values which are present in given arrays, use /* Array
75. The next examples will show how it works:1 print_r($first);88 2 print_r($first);90 3 4 print_r($first);93 5 print_r($first);95 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 print_r($first);98 $first = array(10, "Apple", 20, -18, "Monkey");4 300 Do the Math With Array ValuesUse /* Array
76 to get a sum of array values, /* Array
77 to multiply them, or create your own formula with /* Array
78:1 302 2 3 305 4 307 5 $first = array(10, "Apple", 20, -18, "Monkey");0 310 $first = array(10, "Apple", 20, -18, "Monkey");2 312 $first = array(10, "Apple", 20, -18, "Monkey");4 314 To count all the values of an array, use /* Array
79. It will give all unique values of a given array as keys and a count of these values as a value:1 316 2 318 3 4 321 5 $first = array(10, "Apple", 20, -18, "Monkey");0 (
3$first = array(10, "Apple", 20, -18, "Monkey");2 (
5$first = array(10, "Apple", 20, -18, "Monkey");4 328 $first = array(10, "Apple", 20, -18, "Monkey");6 330 $first = array(10, "Apple", 20, -18, "Monkey");8 332 20 $first = array(10, "Apple", 20, -18, "Monkey");02 Generating ArraysTo generate an array with a given size and the same value, use /* Array
80:1 336 2 338 To generate an array with a range of keys and values, like hours in the day or letters, use range(): 1 340 2 342 3 4 345 5 347 To get a part of an array—for example, just the first three elements—use /* Array
81:1 349 2 351 3 353 If you ever want to generate an associative array with different keys and the same value assigned to each key, you can simply use the /* Array
82 function.1 355 2 357 3 4 360 5 $first = array(10, "Apple", 20, -18, "Monkey");0 /* Array
$first = array(10, "Apple", 20, -18, "Monkey");2 (
$first = array(10, "Apple", 20, -18, "Monkey");4 367 $first = array(10, "Apple", 20, -18, "Monkey");6 369 $first = array(10, "Apple", 20, -18, "Monkey");8 371 20 $first = array(10, "Apple", 20, -18, "Monkey");9 Sorting ArraysIt is good to remember that every sorting function in PHP works with arrays by a reference and returns true on success or false on failure. There's a basic sorting function called /* Array
83, and it sorts values in ascending order without preserving keys. The sorting function can be prepended by the following letters:
You can see the combinations of these letters in the following table: akrua /* Array
84/* Array
85/* Array
86k/* Array
87/* Array
88r /* Array
85/* Array
88/* Array
91u /* Array
86/* Array
93Combining Array Functions Like a BossThe real magic begins when you start to combine array functions. Here is how you can trim and remove empty values in just a single line of code with /* Array
60 and /* Array
69:1 375 2 3 378 4 380 To create an id to a title map from an array of models, we can use a combination of /* Array
40 and /* Array
67:1 382 2 3 385 4 387 5 389 $first = array(10, "Apple", 20, -18, "Monkey");0 391 To get the top three values of an array, we can use /* Array
79, /* Array
85, and /* Array
81:1 393 2 3 396 4 398 5 /* Array
00$first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 /* Array
03$first = array(10, "Apple", 20, -18, "Monkey");4 (
3$first = array(10, "Apple", 20, -18, "Monkey");6 (
5$first = array(10, "Apple", 20, -18, "Monkey");8 /* Array
0920 /* Array
1121 /* Array
1323 $first = array(10, "Apple", 20, -18, "Monkey");02 It's easy to use /* Array
76 and /* Array
69 to calculate the sum of order in a few rows:1 /* Array
172 /* Array
193 /* Array
214 /* Array
235 $first = array(10, "Apple", 20, -18, "Monkey");92 $first = array(10, "Apple", 20, -18, "Monkey");0 $first = array(10, "Apple", 20, -18, "Monkey");2 /* Array
28$first = array(10, "Apple", 20, -18, "Monkey");4 /* Array
30$first = array(10, "Apple", 20, -18, "Monkey");6 /* Array
32$first = array(10, "Apple", 20, -18, "Monkey");8 20 /* Array
35ConclusionAs you can see, knowledge of the main array functions can make your code much shorter and more readable. Of course, PHP has many more array functions, and even the given functions have many variations to use with extra parameters and flags, but I think that in this tutorial we've covered the basics that every PHP developer should know. Learn PHP With a Free Online CourseIf you want to learn PHP, check out our free online course on PHP fundamentals! In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database. |