PHP – Arrays

What is array in php

Arrays in PHP are important part of programming languages. it provides a strong set of such efficient ways to deal with array data type. Arrays are complex variables that allow us to store more than one and a group of values under a single variable

Types of Arrays in PHP

There are three types of arrays that you can create. These are:

  • Indexed array — Indexed array with a numeric key.
  • Associative array —Associative array where each key has its own specific value.
  • Multidimensional array — Multidimensional array containing one or more arrays within itself.

1.Indexed Arrays

Numeric array stores each array element with a numeric index. By default array index starts from zero.The following examples shows two ways of creating an indexed array

<?php
// Define an indexed array
$colors = array(“webtechsource”, “design”, “development”);
?>
This is equivalent to the following example, in which indexes are assigned manually:

<?php
$colors[0] = “webtechsource”;
$colors[1] = “design”;
$colors[2] = “development”;
?>

2. Associative Arrays

In an associative array, the keys assigned to values can be arbitrary and user defined strings.This is the most frequently used type of Arrays in php whose elements are defined in key/value pair.Example:


$webtechsource = array();
$webtechsource[“key1”] = “value 1”;
$webtechsource[“key2”] = “value 2”;
$webtechsource[“key3”] = “value 3”;


<?php

// Define an associative array

$webtechsource = array(“key1″=>value 1, “key2″=>value 2, “key3″=>value 3);

?>

3. Multidimensional Arrays

The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on.

An example of a multidimensional array in php will look something like this:

 
<?php
// Define a multidimensional array
$userdata = array(
  array(
      “name” => “dayanand”,
      “email” => “sainidaya@gmail.com”,
  ),
  array(
      “name” => “webtechsource”,
      “email” => “webtechsource18@gmail.com”,
  ),
  array(
      “name” => “suchitra saini”,
      “email” => “suchitrasaini9@gmail.com “,
  )
);
// Access nested value
echo “Dayanand Email-id is: ” . $userdata[0][“email”];
?>
Common PHP Array Functions

I am going to run through some commonly used array functions.
1 . array_pop() – removes the last element
Example $aArray = array(“a”,”b”,”c”);
array_pop($aArray);
print_r($aArray);
Result Array ( [0] => a [1] => b )
 

2 . array_push() – adds element to the end
Example $bArray = array(“a”,”b”,”c”);
array_push($bArray,”d”);
print_r($bArray);
Result Array ( [0] => a [1] => b [2] => c [3] => d )

  1. array_search() – searches for value and returns its index

Example $cArray = array(“a”,”b”,”c”);
echo array_search(“b”,$cArray);
Result 1

  1. array_shift() – removes the first element and returns it

Example $dArray = array(“a”,”b”,”c”);
echo array_shift($dArray);
print_r($dArray);;
Result b [1] => c )

  1. array_slice() – returns selected elements

Example $eArray = array(“a”,”b”,”c”);
print_r(array_slice($eArray,1,1));
Result Array ( [0] => b [1] => c )

  1. array_splice() – replaces specified elements with new

Example $fArray = array(“a”,”b”,”c”);
$gArray = array(“d”,”e”,”f”);
array_splice($fArray,1,2,$gArray);
print_r($fArray);
Result Array ( [0] => a [1] => d [2] => e [3] => f )
7.array_unique() – removes duplicate values
Example $hArray = array(“a”,”b”,”b”,”c”,”c”,”c”);
print_r(array_unique($hArray));
Result Array ( [0] => a [1] => b [3] => c )

8.array_unshift() – adds element to the beginning
Example $iArray = array(“b”,”c”,”d”,);
array_unshift($iArray,”a”);
print_r($iArray);
Result Array ( [0] => a [1] => b [2] => c [3] => d )

 

  1. count() – counts all elements

Example $jArray = array(“a”,”b”,”c”,”d”);
echo count($jArray);
Result 4

 

  1. sort() – sorts the array

Example $kArray = array(“c”,”b”,”a”,);
sort($kArray);
print_r($kArray);
Result Array ( [0] => a [1] => b [2] => c )

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

+ 43 = 49

%d bloggers like this: