Array function in PHP

Arrays are sets of data which can be defined in a PHP Script. Arrays can contain other arrays inside of them without any restriction (hence building multidimensional arrays). Arrays can be referred to as tables or hashes.

Syntax:

Arrays can be created in two ways. The first involves using the function array. The second involves using square brackets.


The array function method :

In the array function method, you create a array in the scheme of:
$foo = bar()
For example, to set the array up to make the keys sequential numbers (Example: "0, 1, 2, 3"), you use:
$foobar = array($foo, $bar);
This would produce the array like this:
$foobar[0] = $foo
$foobar[1] = $bar
It is also possible to define the key value:
$foobar = array('foo' => $foo, 'bar' => $bar);
This would set the array like this:
$foobar['foo'] = $foo
$foobar['bar'] = $bar

The square brackets method :

The square brackets method allows you to set up by directly setting the values.
For example,to make $foobar[1] = $foo, all you need to do is:
$foobar[1] = $foo;
The same applies for setting the key value:
$foobar['foo'] = $foo;

    <?php
    $array =
    array("name"=>"priya","occupation"=>"IT professional","qualification"=>"BSC-IT","religion"=>"Humanity");
    $array2 = array("priya","IT professional","BSC-IT","Humanity");
    $array3 = array("name"=>"priya","IT professional","colour"=>"BSC-IT","Humanity");
    print_r($array);
    print_r($array2);
    print_r($array3);
    ?>
     


PHP Output:

    Array ( 
    [name] =>Priya
    [occupation] => IT professional 
    [qualification] => BSC-IT 
    [religion] => Humanity
     )
     Array (
    [0] =>priya
    [1] => IT professional 
    [2] => BSC-IT 
    [3] => Humanity
     ) 
    Array ( 
    [name] =>Priya
     [0] => IT professional
     [colour] => BSC-IT 
    [1] => Humanity 
    )

     


Multidimensional Arrays :

Elements in an array can also be an array, allowing for multidimensional arrays.

https://www.w3schools.com/php/php_arrays_multidimensional.asp

Array Functions :

There exist dozens of array manipulation functions. Before implementing your own, make sure it doesn't already exist as a PHP function in Array functions (PHP manual entry).

Array traversal :

In various circumstances, you will need to visit every array element and perform a task upon it.
The simplest and the most widely used method for this is the foreach operator which loops through the whole array and works individually with each key/item couple. If a more complex way of traversing the array is needed, the following functions operate using the internal array pointer:
reset - sets the internal pointer to the first element and returns the first element
prev - sets the internal pointer to the previous element and returns it
current - returns the current element; does not change the internal pointer
next - sets the internal pointer to the next element and returns it
each - returns the current element; then sets the internal pointer to the next element
end - sets the internal pointer to the last element and returns the last element


    <?php
    // using an array's iterator to print its values in reverse order
    $my_array = array('a', 'b', 'c');
    end($my_array); 
    while($i = current($my_array)){
    echo $i."\n"; 
    prev($my_array);
    } 
    ?>

     

In various circumstances, you will need to visit every array element and perform a task upon it.
Another possibility is defining a function and applying it to each array element via one of the following functions:
array_walk - applies a function to each array element.
array_walk_recursive - same, but if the element is itself an array, it will traverse that array too.


Did you like our works?

Here is your content of the callout box lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.