Useful PHP Array methods you should know

Part 2 – Useful PHP Array methods you should know

As a PHP developer or learning PHP, once in a while, you have to work with arrays. So while working on array sometimes you have to do manipulation on array data and you write a bunch of code for that whereas PHP already has done the hard part for you. PHP has built many array methods that can be used with the array. Let’s see some useful PHP array methods you should know.

PHP has a lot of built-in methods for arrays. In this article, we will see a few most useful ones. You can learn more about these functions on PHP’s official website here.

If you know there are two types of array in PHP.

Associative Array (key=>value)

array(0 => 100, "color" => "red");

Indexed Array (use index position as key)

array(100,"red");

Let’s have a quick list of array methods we are going to see in this article with examples.

  • in_array()
  • array_push()
  • array_pop()
  • sizeof() / count()
  • array_merge()

Now let’s see them individually with a short explanation.

in_array()

Most of the time you might have to use in_array() function. Basically You will use it when you have to check some value that exists in your array or not. It returns boolean value (true/false). It accepts two parameters, the first parameter is the value that you are searching for, and the second the array of data in which you want to find the first parameter.

Lets see an example to make it more clear.

$os = array("Mac", "Windows", "Linux");

if (in_array("Windows", $os)) {
    echo "Got Windows";
}

array_push()

array_push() is used when you have an array and you want to add another value in that array. Basically we push another value in end of the array.

Example:

$os = array("Mac", "Windows", "Linux");

$myOS = "newOS";
array_push($os, $myOS);
print_r($os);

//output
Array
(
    [0] => Mac
    [1] => Windows
    [2] => Linux
    [3] => newOS
)

array_pop()

array_pop() is used when you have an array and you want to remove/delete last value from that array. Basically we can remove the last value from array using array_pop() function. It accepts only one parameter and that is the array data itself.

Lets see an example.

$os = array("Mac", "Windows", "Linux");

array_pop($os);//once
array_pop($os);//twice
print_r($os);

//output
Array
(
    [0] => Mac
)

sizeof() / count()

These two functions used for single purpose and that is to count the how many values are available in the array. It accepts one parameter that it array itself. It returns the count of array in integer.

lets see an example

$os = array("Mac", "Windows", "Linux");

echo sizeof($os);
echo count($os);
//output
3
3

array_merge()

As we can guess by its name it is used to merge two or more arrays. Array merge accepts two or more parameters but lets stay with two arrays. So the first parameter will be one in which second parameter will append.

Example

$os = array("Mac", "Windows", "Linux");
$users =array("macUser","LinuxUser");
$merged = array_merge($os,$users);

print_r($merged);

//output
Array ( [0] => Mac [1] => Windows [2] => Linux [3] => macUser [4] => LinuxUser )

Note: If we are working with associative array (key,value) and if both array have same keys with different value then first keys value will be overwriiten. Lets see with example because this is important.

with same key and diffrent value

$array1 = array(1 => "data");
$array2 = array(1 => "data2");
$result = array_merge($array1, $array2);
print_r($result);

//output
array( [1] => data2 )

If you dont want to over write this you can alternatively use + operator to merge two arrays.

Example:

$array1 = array(1 => "data");
$array2 = array(1 => "data2");
$result =$array1 + $array2;
print_r($result);

//output
Array ( [1] => data )

So that was all about few useful PHP Array methods you should know , we will continue with some more useful stuffs in PHP.

If you have any doubts or If I have missed something let me know in the comment box.

Want to know few more important methods of PHP arrays. I have created another part of it.

Part 2 – Useful PHP Array methods you should know

See also

Leave a Reply

Your email address will not be published. Required fields are marked *