What is Array in PHP?

Array is an important part in PHP and plays an indispensable role in creating any webpage. If you have any doubt in the use of Array in PHP than you should definitely read this blog.  We have created this blog to explain the use of Array in PHP in a easy way. Here I’m going to start with a small introduction of PHP.

What is PHP?

PHP (Hypertext Preprocessor) is a server scripting language and it is influential tool for creating dynamics and interactive web pages, images very quickly. With the help of PHP you can build a strong and highly rich featured webpage.

Learning PHP can become very easy and you can enjoy it if you will go into its depth. Array is one of the most essential part of this server scripting language. Let us know about the Array in depth.

What is Array?

An Array is a data structure that stores one or a lot of values in a single value. For knowledgeable programmers it’s necessary to notice that PHP Arrays are literally maps (each key is mapped to a value).

Array is a traditional PHP variables however we can put alternative variables within it. Every variable within an Array is called a component in PHP. Every element has a key and a value, which can be any other variable. An Array is a special variable, which might hold more than one value at a time.

Using Arrays in PHP? 

Arrays can be utilized in many ways to store and organize data quickly and efficiently. It is one of the more helpful data sorts available to any programming language.

Arrays can most simply be represented as an ordered list of components. You can access the individual components by referring to their index position within the Array. The position is either such numerically or by name in PHP. An Array with a numeric index is usually known as an indexed Array while one that has named positions is called an associative Array. All Arrays are associative, but you can use a static numeric index to connect them in PHP also.

Types of Array in PHP

  • Indexed Array
  • Associative Array
  • Multidimensional Array

Indexed Array – where each element is referenced by a numeric index, usually starting from zero. For example, the first element has an index of 0, the second has an index of 1, and so on.

Example

The Basic example.

<?php
     $myarray = array(“PHP”, “Java”, “Python”);
     print_r($myarray);
?>

The code above generates the following result.

Array
(
     [0] => PHP
     [1] => Java
     [2] => Python
)

Associative Arrays – each element is referenced by a string index. Associative Arrays are Arrays in PHP that use named keys that you assign to them.

Example

The following code creates a PHP associative Array to hold the key value pair.

<?php
     $myarray = array(“a”=>”Apple”, “b”=>”Bag”, “c”=>”Cat”);
     var_dump($myarray);
?>

The code above generates the following result. 

Array (size=3)
   ‘a’ => string ‘Apple’ (length = 5)
   ‘b’ => string ‘Bag’ (length = 3)
   ‘c’ => string ‘Cat’ (length = 3)

Multidimensional Array: A multidimensional Array is an Array containing one or more Arrays. An Array that contains other Arrays is a two-dimensional Array. If those Arrays also contain Arrays, then the top-level Array is a three-dimensional Array, and so on.

Example

The following code uses Array operator ([]) to create an two-dimensional Array. The key of the first level is Java and PHP.                        

<?php
     $book[‘Java’] = array(“Name”=<”Java Book”, “Price”=> 1.2, “OnSale”=> “No”);
     $book[‘PHP] = array(“Name”=<”PHP Book”, “Price”=> 5.3, “OnSale”=> “Yes”);
     var_dump($book);
?>

The code above generates the following result.

array (size=2)
   'Java' =>
     array (size=3)
       'Name' => string 'Java Book' (length=9)
       'Prince' => float 1.2
       'OnSale' => string 'No'(length=2)
   'PHP' =>
     array (size=3)
       'Name' => string 'PHP Book' (Length=8)
       'Prince' => float 5.3
       'OnSale' => string 'Yes' (Length=3)

Sorting Array in PHP: The elements in an Array can be sorted in alphabetical or numerical order, descending or ascending.

Example

The code above generates the following result. 

Array
(
          [0] => CSS
          [1] => HTML
          [2] => Java
          [3] => PHP
)

rsort(): The rsort() function sorts an indexed Array in descending order.

Example

The code above generates the following result:

Array
(
          [0] => PHP
          [1] => Java
          [2] => HTML
          [3] => CSS
)

asort(): sort associative Arrays in ascending order, according to the value.

Example

The code above generates the following result.

Array
(
          [PHP] => 5
          [Java] => 7
)

ksort() – sort associative Arrays in ascending order, according to the key

Example

The code above generates the following result.

Array
(
      [A] => Z
      [B] => X
      [C] => Y
)

arsort() – sort associative Arrays in descending order, according to the value.

Example

The code above generates the following result.  

Array
(
      [Key1] => Z
      [Key3] => Y
      [Key2] => X
)

krsort() – sort associative Arrays in descending order, according to the key. This is the opposite of the ksort().

Example

The code above generates the following result.   

Array
(
      [C] => Y
      [B] => X
      [A] => Z
)

PHP Array Functions

  • PHP Array_change_key_case() function: The Array_change_key_case() function changes all keys in an Array to lowercase or upper case.

Example

The code above generates the following result.  

Array
(
      [A] => a
      [B] => b
      [C] => c
)

Array_chunk() function: The Array_chunk() function splits an Array into chunks and stores the chunks into new Arrays.

Example

Split an Array into chunks of two.

The code above generates the following result.    

Array
(
      [0] => Array
      (
            [0] => A
            [1] => B
     )
    [1] => Array
    (
            [0] => C
            [1] => D
    )
)

Array_combine() function:  The Array_combine() function creates an associative Array by using the elements from “keys” Array and “values” Array.

Example

Create an Array by using the elements from one “keys” Array and one “values” Array

The code above generates the following result.    

Array
(
       [Python] => 5
       [Java] => 7
       [HTML] => 3
)

PHP Array_count_values() Function: The Array_count_values() function counts the values of an Array.

Example

Count all the values of an Array

The code above generates the following result.    

Array
(
       [A] => 2
       [Cat] => 1
       [Dog] => 2
)

I know that I have touched only the tiny surface of a big iceberg. There are many more functions of Array in PHP but I could cover only few here. If you are interested in knowing more about all the functions of Array in PHP then please visit www.php.net.

Hope you will like this post on “Array in PHP”.

Leave a Reply

You must be logged in to post a comment.

Copy link