PHP array() construct Function
×


PHP array() cconstruct Function

3214

In order to create an array in PHP, we can use array() constructor, this will build an array from the arguments passed. This constructor can be used to initialize indexed as well as associative arrays.

In the example given below, we will demonstrate how to build both types of the array with the help of an array() constructor.

Note:

array() is a constructor used to build arrays and is not a regular function.

Syntax:

Indexed array:

array(val1,val2,.......,valn);nn

This will create an indexed array with the starting value of the index as 0.

val1,val2,val3 and valN represents the  array items.

Associate array:

array(key1=>val1,key2=>val2,……..,keyN=>valN);

To create an associative array, "=>" is used for separating the keys and values.

Key1, key2, keyN represents the keys of array items in case of associative arrays.

Example:

<?php
$indexed_array=array('Mango','Apple','Orange','Banana');
$asso_array=array('fruit1'=>'Mango','fruit2'=>'Apple','fruit3'=>'Orange','fruit4'=>'Banana');
echo "<pre>";
echo "Indexed array is: <br>";
print_r($indexed_array);
echo "<be> Associative array is: <br>";
print_r($asso_array);
?

Output:

Output PHP Array Construct Function



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments