login

PHP Array Operations

Sometimes in your program, you want to check whether a variable is an array or not. PHP provides you the is_array() function to help you to do so. is_array() function accept a variable. If the variable is an array type, it returns true otherwise it returns false. Here is the code sample of using is_array() function.

 
  <?php 
  $myarray = array("php","array","is cool");
  
  if(is_array($myarray))
      echo "myarray is an array <br/>";
  else 
      echo "myarray is not an array <br/>";
  
 $size = 3;
 if(is_array($size))
     echo "size is an array <br/>";
 else 
     echo "size is not an array <br/>";
 ?>

Adding / removing element to/from an array

In order to add a new element to an array, by PHP language natural, you just add it by assigning an element's key to a value. If the key is numerical index you can assign it without using key value. The key value is automatically increased in this case. Here is code snippet:

 <?php 
    // adding new elements 
    $state["ca"] = "California";
    $state["ta"] = "Texas";
 
    // adding new elements by using numerical index key 
    $order[] = "add";
    $order[] = "remove";
 ?>

PHP also provides built-in function array_ push() to allows you to add one or more than one elements onto array at the same time. array_push() function returns true on success, otherwise false. Here is the array_ push() signuature:

int array_push(array_to_add,element1,[element2...]);

We can use the the push function as follows:

 <?php 
      $state[] = "New York";
      $is_ok = array_push($state,"California","Texas");
      echo "Elements added to the end of array is ".$is_ok."<br/>";
 ?>

To add an element to the begin of an array, you can use array_unshift() function.

<?php 
      $states = array("California","Texas");
      array_unshift($states,"New York");
      echo $states[0];// New York 
 ?> 

To remove the last element of an array you use the the function array_pop(). array_pop returns the last element's value. Here is the signature of function array_pop():

element_value array_pop(array_to_remove)

You can use array_pop function as follows:

<?php 
      $states = array("California","Texas");
      $s = array_pop($states); // $s = Texas 
      echo $s;
?> 

To remove the first element from an array you use the function array_shift().

 <?php 
      $states = array("California","Texas");
      $s = array_shift($states); // $s = California 
      echo $s;
 ?>

Searching for array element

Checking array element

PHP provides in_array() function to check whether an element is in array or not. The in_array() function signature is as follows:

boolean in_array(element_to_check, array, [boolean strict]);

You pass element to check and array variable to function. If element found in_array() function returns true othewise it returns false. There is optional parameter called strict. If you pass this parameter as true, it will not only check the value but also check the data type of element_to_check.

 <?php 
       // init 
       $salary = array(56000,72000,80000,120000);
       $elem = 120000;
       $another = "56000";
       // check 
       $t1 = in_array($elem,$salary);
       echo $t1; // 1 
       
      $t2 = in_array($another,$salary,true);
      echo $t2; // "blank" 
 
 ?>

The second check is failed because the array element is integer type and the variable to check is string type.

To check whether a key is existed in an array, you can use array_key_exists() function

boolean array_key_exists(key_to_check, array)

The function array_key_exists() returns true if the key_to_check is found in array, otherwise returns false. You can use the function as follows:

 <?php 
     // array of languages 
     $language = array(
          "Franche" => "French",
          "England" => "English",
          "India" => "Indian",
          "China" => "Chinese" 
      );
  
     $key_exist = array_key_exists("China",$language);
     echo $key_exist; // 1 
 
     $key_exist = array_key_exists("Germany",$language);
     echo $key_exist; // blank 
 ?>

Sometime you want to get a set of specific keys in an arrray, you can use array_keys() function to do so.

array array_keys(array_to_get, [key1,...]);

the function return an array which contain all the keys. If you want to get a set of specific keys you can pass them to key1,... parameters.

<?php 
      // array of languages 
     $language = array(
          "France" => "French",
          "England" => "English",
          "India" => "Indian",
          "China" => "Chinese" 
      );
  
     $country = array_keys($language);
     // Array ( [0] => France [1] => England [2] => India [3] => China ) 
 ?>

If you want to get all values of an array you can use function array_values()

array array_values(array_to_get)

This function is useful when you want to convert the associative array values into numerical index values.

 <?php 
      // array of languages 
      $language = array(
          "France" => "French",
          "England" => "English",
          "India" => "Indian",
          "China" => "Chinese" 
      );
  
     $lang = array_values($language);
     // Array ( [0] => French, 
     //         [1] => English, 
     //         [2] => Indian, 
     //         [3] => Chinese ) 
 ?>