What are the array functions in PHP

PHP functions for arrays

Information about the article

categoryPHP
authorStefan
date07.02.2019

is_array ()

Let's start with the simplest Array function an: The PHP function is_array () tests, whether a variable holds an array. The variable to be tested must be transferred as a parameter to the function.

If the test is positive, true or 1 is returned. Otherwise false or nothing. An example:

We define two variables in the first two lines. One ($ anArray) holds an array with integer values. The other variable ($ noArray) (surprise!) Does not hold an array, but a string.

We now run the function in lines 4-5 is_array () twice off. Since the variable $ einArray holds an array, the function in line 4 returns true, ie 1. In line 5, however, the function returns false and thus nothing is output.

So we see the following on the screen:

1

count ()

Let's move on to the next very useful feature. count ()counts the elements of an array and returns the result as an int value. We write our array variable that is to be counted in brackets.

Example:

The echo output is accordingly 4, because the array has 4 elements.

var_dump ()

We already got to know this function with the functions for handling variables. It helps us particularly with arrays, as it outputs all of their contents - and nicely formatted.

We test the function once with an associative array:

The result looks like this:

array (4) {["cherries"] => float (2.99) ["strawberries"] => float (3.49) ["grapes"] => float (1.5) ["oranges"] => float (4.19)}

shuffle()

With this function the elements of an array randomly mixed.

In the following example we define a numerically indexed array (line 3) and mix in the individual elements shuffle() neatly through (line 4).

Finally, we use the var_dump () function in line 5 to see the result of the completely new order of the array elements:

array (7) {[0] => int (3) [1] => int (4) [2] => int (5) [3] => int (6) [4] => int (1) [5] => int (7) [6] => int (2)}

array_reverse ()

The last will be the first - no problem with this function. array_reverse () namely simply reverses the order of the array elements. The last element becomes the first and vice versa.

The function leaves the original array untouched and instead returns a newly created one:

With the function call array_reverse ($ captains); the order of the elements is reversed and saved as a new array in the $ captainsNeu variable. We then call up the individual elements individually and receive the following output:

Sisko - Kirk - Janeway - Picard

in_array ()

With in_array () we can check whether a certain value is in a certain array.

The function requires two parameters: once the searched value and once the array in which this value is to be searched for. The first value will too needle called, in German "needle" - with this function we can literally look for the needle in the haystack.

When we have found it, the function returns true or 1.

As always, an example makes things clear:

All right? We check in line 5 with the in_array ()-Function whether the value of $ planet ("Mars") exists in the $ planetenArray array.

We wrote the function as a test in an if-else query. Depending on the Boolean result value, we get one of the two echo texts displayed on the screen. In our case (because true): "Yes, this planet exists!"

explode ()

Now it's getting violent! Because with the function explode () you can use any Blow up the string (English explode) and the dismembered Store individual parts in an array. Does that sound martial? Well, not so bad, because the original string is completely retained.

What we have to ask ourselves when using the function is with which separator (delimiter) the string is to be broken down. The space '' is often useful, as such a string can be broken up after its words.

The separator is the first function parameter. The string that we want to break into individual parts is the second parameter:

Calling the function var_dump () at the end of line 7 shows us the individual array elements that we received from the string $ string:

array (8) {[0] => string (3) "I" [1] => string (3) "am" [2] => string (4) "more" [3] => string (3) "as" [4] => string (3) "the" [5] => string (5) "sum" [6] => string (6) "my" [7] => string (5) "parts "}

In the end, the proof that nothing really happened to our string:

That makes:

I am more than the sum of my parts

implode ()

The opposite of explode () is implode (). This function causes the individual parts of an array put together into a string become.

We pass two parameters to the function. The first is a string. This string (usually a single character is sufficient) is the part that stands between our assembled individual parts. That's why it is also called $ glue, which means something like "glue". And that's exactly what we do with the function implode () also: We glue a string together from individual parts!

The second parameter is the array whose elements will be joined to form the new string. The return value is then this new string.

Again a specific code example will show us how this works:

The output looks like this:

Colored falcon, gray falcon, silver falcon, peregrine falcon

By using the function (second line) we can get a composite string from the individual parts of the $ falken array. We use, (comma and space) as the "glue" string.

array_pop ()

The function array_pop () returns the last element of an array. This element is after deleted from the array:

In this example we create a string array and then call the function array_pop () on. The function receives the array $ falken as a parameter. The last array element (Silberfalke) is returned (and displayed with an echo on the screen). This also ends the existence of this array element, because it is now deleted from the array, as we can prove with the var_dump () function:

Last element: Peregrine falcon array (3) {[0] => string (9) "Buntfalke" [1] => string (9) "Gray falcon" [2] => string (11) "Silberfalke"}

We see: the array no longer has a peregrine falcon!

array_shift ()

This function works almost exactly like array_pop () - just the other way round: Instead of the last array element, the first is returned and then deleted from the array.

In the following example we recreate the array with strings from above. But this time we bet array_shift () a:

First element: Buntfalke array (3) {[0] => string (9) "Graufalke" [1] => string (11) "Silberfalke" [2] => string (11) "Peregrine falcon"}

write a comment

Only registered and activated users can comment.
Sign in with my account

All comments

There are no comments yet on this topic.