PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Define An Array

An array in PHP is a type of data structure that allows you to store multiple values in a single variable. Here's how you can define an array:

  • Indexed Array

An indexed array is a simple list of items in a specific order. The index of each item (starting from 0) is generated automatically. You can define an indexed array using the array() function or the square bracket [] syntax.

$fruits = array("Apple", "Banana", "Cherry");

// or

$fruits = ["Apple", "Banana", "Cherry"];

You can access elements of an indexed array like this:

echo $fruits[0];  // Outputs: Apple
  • Associative Array

Associative arrays are arrays that use named keys instead of numeric keys. You can create them using the array() function or the [] syntax.

$ages = array("John" => 35, "Jane" => 32, "Bob" => 29);

// or

$ages = ["John" => 35, "Jane" => 32, "Bob" => 29];

In this example, the names are the keys, and the ages are the values. You can access the values by referring to the keys:

echo $ages["John"];  // Outputs: 35
  • Multidimensional Array

A multidimensional array is an array containing one or more arrays. PHP understands multidimensional arrays that are two, three, four, five, or more levels deep.

$persons = array(
    "John" => array(
        "age" => 35,
        "email" => "john@example.com"
    ),
    "Jane" => array(
        "age" => 32,
        "email" => "jane@example.com"
    )
);

You can access elements of a multidimensional array like this:

echo $persons["John"]["age"];  // Outputs: 35

Remember that PHP arrays can contain different types of elements, so you can have an array that contains a combination of numbers, strings, and other arrays.

  1. PHP Array Initialization:

    • Description: Array initialization refers to the process of creating an array.
    • Example Code:
      $numbers = array(1, 2, 3, 4, 5);
      
  2. PHP Indexed Array Declaration:

    • Description: An indexed array is a simple array where each element is assigned a numeric index.
    • Example Code:
      $fruits = array('Apple', 'Banana', 'Orange');
      
  3. PHP Associative Array Declaration:

    • Description: An associative array uses named keys instead of numeric indices.
    • Example Code:
      $person = array(
          'name' => 'John',
          'age' => 25,
          'city' => 'New York'
      );
      
  4. PHP Define Array with Values:

    • Description: You can define an array with values directly during initialization.
    • Example Code:
      $colors = ['Red', 'Green', 'Blue'];
      
  5. PHP Initialize Empty Array:

    • Description: You can create an empty array and later add elements to it.
    • Example Code:
      $emptyArray = array();
      // OR
      $emptyArray = [];
      
  6. PHP Dynamic Array Declaration:

    • Description: Dynamic array declaration allows you to add elements to an array dynamically.
    • Example Code:
      $dynamicArray = array();
      $dynamicArray[] = 'Element 1';
      $dynamicArray[] = 'Element 2';
      
  7. PHP Array Assignment:

    • Description: Assigning values to an array involves specifying the index or key.
    • Example Code:
      $numbers[0] = 10; // Assigning a value to the first element of an indexed array
      
      $person['occupation'] = 'Engineer'; // Assigning a value to the 'occupation' key in an associative array