Arrays in PHP

ad, authenticate, script, geeks corner, json, image, picture, server, file, write, read, close, fread, readfile, fclose
PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

An array in PHP is actually an ordered map. A map is a type that associates values to keys. In some programming languages such as PHP and Python, arrays can hold any value type. In other languages such as Java, the data type has to be declared when declaring the array. In PHP, arrays are treated as ordered maps. An ordered map is a key value pair .

In PHP, we don't have to declare the type of data held in the array since it is a loosely typed language. This holds true for Python also. In Python and PHP, we can have any combination of value types in the array. In strongly typed languages such as Java, the value data type must be defined when declaring an array. In PHP, we are not bound by having just integer indexes, we can index by strings also. In PHP keys can be either string or integer types. In PHP, if a key is not provided, it will give an ascending integer as the keys to the element. Below are the differences in how arrays are declared in each language. In Java and Python, the index of the array is an ascending integer.

PHP

    $array1 = array(
        1 => "a",
        2 => "b",
    ) ;

    $array2 = array["Mary",2,"lamb");

   $array3 = array(
       "name" => "John",
       "age" => 36,
  ) ;


Python
    array1 = ["a","b","c"]
    array2 = ["Mary", 2, "lamb"]


Java
    int myArray[] = new int[2] ;
    myArray[0] = 1 ;
    myArray[1] = 2 ;


In PHP, we can use any string or integer as an index for the array. We can also have the array start at any index by providing a different key. In Python, the index will start at 0 and can't have strings as indexes. in Java, the data type and size of the array must be declared. In PHP and Python, we don't have to declare the size nor the data type, in fact, we can have the elements hold any data type we desire. Also note that in Java, an array must be instanced, as we can see by the "new" keyword, this is not necessary in PHP or Python.

PHP

1    $array1 = array(
2        1 => "a",
3        2 => "b",
4    ) ;
5
6    $array2 = array["Mary",2,"lamb");
7
9   $array3 = array(
10       "name" => "John",
11       "age" => 36,
12   ) ;
13
14   # foo1 will hold "Mary"
15   $foo1 = $array2[0] ;
16
17   # foo2 will hold "a"
18   $foo2 = $array1[1];
19
20   # foo3 will hold 36
21   $foo3 = $array3["age"] ;


Python
1    myArray = ["a","b","c"]
2    myArray2 = ["Mary", 2, "lamb"]
3
4    # foo1 will hold "a"
5    foo1 = myArray[0]
6
7    # foo 2 will hold 2
8    foo2 = myArray2[1]


In PHP and Python, we can add and remove elements to an array. In Java, since the size of the array is set, in order to add to an array, we have to create a new, larger array and vice versa to remove items from an array. PHP has several methods to remove items from an array such as array_splice(), unset() and array_pop(). The unset() method is a more generic method that isn't specific to arrays, it is used to delete a variable of any kind and an element of an array is nothing more than a variable. In Python, we have different methods to remove items from an array: remove(), pop() and clear(). Since the size of the array is fixed in Java, there is no way to decrease or increase the size of the array.

PHP

1    $array1 = array(
2        1 => "a",
3        2 => "b",
4    ) ;
5
6    $array2 = array["Mary",2,"lamb");
7
9    $array3 = array(
10       "name" => "John",
11       "age" => 36,
12       "address" => "any town",
13   ) ;
14
15   # array1 will hold ["a"]
16   array_pop($array1) ;
17
18   # array2 will hold ["Mary","lamb"]
19   unset($array2[1]);
20
21   # array will hold ["John","any town"
22   array_splice($array3, 1, 1);


Python
1    myArray = ["a","b","c"]
2    myArray2 = ["Mary", 2, "lamb"]
3
4    # myArray will hold ["b","c"]
5    myArray.pop(0)
6
7    # myArray2 will hold ["Mary",2]
8    myArray2.remove("lamb")
9
10   #myArray2 will be empty
11   myArray2.clear()

If we think of arrays as a specialized form of ordered maps, moving from Java ( or any other language ) to PHP becomes easier. Ordered maps does everything an array does and then some. When using ordered maps as a way to implement arrays, we will have to be more careful in the use of keys as well as the data types of the values. Since we can increase or decrease the size of ordered maps, we must keep track of the size.