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 Arrays

Arrays in PHP are a type of data structure that allows you to store multiple values in a single variable. An array can hold many values, and the values can be of any PHP type like numbers, strings, or even other arrays.

Creating Arrays

You can create an array in PHP using the array() function, or with the short array syntax []:

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

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

Accessing Array Elements

You can access the values in an array by referring to the index number:

echo $fruits[1];  // Outputs: Banana

Array indices in PHP start from 0, so $fruits[1] refers to the second element in the array.

Associative Arrays

Associative arrays are arrays that use named keys instead of numeric keys. You can create them like this:

$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

Looping Through Arrays

You can use a foreach loop to iterate over an array:

foreach ($fruits as $fruit) {
    echo $fruit . "\n";
}

This will print each fruit on a new line. If you're working with an associative array and you want to access both the keys and the values, you can do it like this:

foreach ($ages as $name => $age) {
    echo "$name is $age years old.\n";
}

Array Functions

PHP provides a lot of functions to work with arrays, like count() to get the number of elements, sort() to sort the elements, array_push() to add an element, and many more. For example:

$fruits = ["Apple", "Banana", "Cherry"];
echo count($fruits);  // Outputs: 3

sort($fruits);
print_r($fruits);  // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry )

array_push($fruits, "Durian");
print_r($fruits);  // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry [3] => Durian )

That's a quick introduction to arrays in PHP. They're a fundamental part of the language, and you'll use them a lot when you're writing PHP code.

  1. PHP associative arrays:

    • Associative arrays use named keys for their elements.
    $person = [
        'name' => 'John',
        'age'  => 30,
        'city' => 'New York',
    ];
    
  2. PHP indexed arrays:

    • Indexed arrays use numeric indices for their elements.
    $colors = ['Red', 'Green', 'Blue'];
    
  3. PHP multidimensional arrays:

    • Multidimensional arrays contain other arrays.
    $matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ];
    
  4. PHP array functions:

    • PHP provides various functions for array manipulation, such as count, array_push, array_pop, array_merge, etc.
    $numbers = [1, 2, 3, 4, 5];
    $count   = count($numbers);
    
  5. PHP array initialization:

    • Initialize an array with values.
    $fruits = ['Apple', 'Banana', 'Orange'];
    
  6. PHP array manipulation:

    • Manipulate arrays using functions like array_push, array_pop, array_shift, array_unshift, etc.
    $colors = ['Red', 'Green'];
    array_push($colors, 'Blue');
    
  7. PHP array sorting:

    • Sort arrays using functions like sort, rsort, asort, ksort, etc.
    $numbers = [4, 2, 8, 1, 6];
    sort($numbers);
    
  8. PHP array merging:

    • Merge arrays using the array_merge function.
    $arr1 = ['a', 'b'];
    $arr2 = ['c', 'd'];
    $merged = array_merge($arr1, $arr2);
    
  9. PHP array iteration:

    • Iterate through arrays using loops or functions like foreach.
    $fruits = ['Apple', 'Banana', 'Orange'];
    
    foreach ($fruits as $fruit) {
        echo $fruit . "\n";
    }