Important Array Functions I used in my Laravel projects

Important Array Functions I used in my Laravel projects

Working with Laravel, I encountered lots of arrays which gave rise to the need for array functions. Array functions allow me to access and manipulate arrays. In this article, I would like to go over some of the important array functions in PHP I used while working on my Laravel project.

In_array

The in_array() is a PHP function that returns true if the value passed as the first parameter exists in the second parameter or otherwise returns false. The second parameter is always an array. The first and second parameters are both required.

<?php

// in_array($first_parameter, $second_parameter)

$arithmetics = array("addition", "divison", "multiplication");

echo (in_array("addition", $arithmetics)) ? "It exist" : "It doesnt exit";

echo '<br>';

echo (in_array("Add", $arithmetics)) ? "In the Array" : "Not in the array";

?>
It exist
Not in the array

There is also an optional third parameter which is of boolean value. If set to TRUE, it searches the first parameter with the same data type as the second parameter. The default value is false.

<?php
// in_array($first_parameter, $second_parameter, $type)

$arithmetics = array("addition", "divison", "multiplication", 23);

echo (in_array("23", $arithmetics, TRUE)) ? "It exist" : "It doesnt exist";

// "23" and 23 are treated differently here under the TRUE Condition but they are the same under the FALSE condition.

?>
It doesnt exist

Array_diff

The array_diff() function is a PHP function that returns an array of values from the first array which doesn't exist in the second array.

<?php

$first_array = array("horse", "cars", "hand");

$second_array = array("cars", "horse");

$result = array_diff($first_array, $second_array);

print_r($result);

?>
Array ( 
    [2] => hand 
)

We can also compare the first array with more than one array.

<?php

$first_array = array("horse", "cars", "hand", "keys", "Toys");

$second_array = array("cars", "horse", "land");

$third_array = array("hand", "land");

$result = array_diff($first_array, $second_array, $third_array);

print_r($result);

?>
Array ( 
    [3] => keys 
    [4] => Toys 
)

Array_push

The array_push() is a PHP function that adds or pushes one or more values into the end of an array. It also returns an updated array after the values have been pushed or added.

<?php
    $arithmetic = array("addition", "substraction");

    array_push($arithmetic, "division", "multiplication");

    print_r($arithmetic);
?>
Array ( 
    [0] => addition 
    [1] => substraction 
    [2] => division 
    [3] => multiplication 
)

Array_intersect

The array_intersect() is a PHP function that returns the value that matches all arrays present as arguments.

<?php

    $person1 = array("a"=>"eye","b"=>"nose","c"=>"ear","d"=>"fingers");

    $person2 = array("e"=>"eye","f"=>"mouth","g"=>"fingers");

    $common_feature = array_intersect($person1, $person2);

    print_r($common_feature);
?>
Array ( 
    [a] => eye 
    [d] => fingers 
)

It takes at least two arrays as arguments which means you can compare with more than one array.

<?php
    $person1 = array("a"=>"eye","b"=>"nose","c"=>"ear","d"=>"fingers");

    $person2 = array("e"=>"eye","f"=>"mouth","g"=>"fingers");

    $person3 = array("h"=>"fingers");

    $common_feature = array_intersect($person1, $person2, $person3);

    print_r($common_feature);
?>
Array ( 
    [d] => fingers 
)

Array_search

The array_search() is a PHP function that looks for a value being passed as an argument in the first parameter, with an array being the second parameter. If it exists, it returns the key of the value or otherwise returns false. It is useful when you want the key to a particular value you are searching for.

<?php
    $person = array("name"=>"John","age"=>22,"color"=>"purple");

    echo array_search("22", $person);
?>
age

The third parameter can be set to TRUE or FALSE and the default value is False. If set to TRUE, the values must be of the same type. If there is more than one match, the first matched key will be returned.

<?php
    $person = array("name"=>"John","age"=>"22","score"=>22);

    echo array_search(22, $person, TRUE);
?>
score

Array_sum

The array_sum() is a PHP function that returns the sum of all values in an array. It performs the same function as the Laravel eloquent sum().

<?php
    $scores = array(98 , 95 , 78);

    echo array_sum($scores);
?>
271

Conclusion

These are some of the important array functions that were useful for me while working with Laravel projects. Are there arrays you have used and were useful, kindly drop them in the comment section.