Home » Programming » php » PHP array types

PHP array types

Mainly two types of Array in PHP

  1. SINGLE ARRAY/ SINGLE DIMATIONAL ARRAY
  2. MULTIPLE ARRAY/ MULTI-DIMAINTIONAL ARRAY
  3. This two types of the array divided into two types of INDEX ARRAY/ NUMERIC ARRAY & ASSOCIATIVE ARRAY.

    Single Array
    In the single array, we can store multiple values in one variable.

    Example

    Like a fruits array store all fruits name.
    All previous examples are single array.

    Numeric Array / Index Array

    In the numeric array, all index value must be integer or number. If you only store data in an array, index automatically starts with zero this kind of array is called numeric or index array. You don’t need to assign index.
    But if you want to address the index value you can, the index must be assigned with integer type of value.

    Example

    Associative Array

    If you assign index value with string and each element can be assigned by unique key this is called Associative array.

    Above example is an associative array.

    Benefit of this array is we can easily recognize the details of the employee or anything (which you store).

    We can also store these details in numeric array

    But you don’t know which value is the name of this employee, which value is the address of this employee and also same as age and designation.

    But in the associative array, we easily find the employee name by its index value which declared by name.
    print_r(), var_dump() and foreach loop used to print an associative array.

    Output

    Multi-dimensional Array or Multiple Array

    In multiple array we can store more than one array in a single variable. So we can manage more than one array in a single stack.
    Like we can store all employee details in a single variable

    Output

    The main array ‘employees’ have 5 index each index store a single array.
    This is also two dimentional array because you can return any employee details by two indexes.
    Like if we want to display the second employee’s designation just print

    Result

    In this way you can create multi-dimensional arrays and store multiple array in one single variable.

Managed by MochaHost - Fast & Easy WordPress Hosting