PHP Arrays

In this tutorial learn PHP arrays : An array in PHP is actually an ordered map. A map is a type that associates values to keys, In simply way array is  a variable which can store more than multiple values. 


Arrays Syntax


array(
    key  => value,
    key2 => value2,
    key3 => value3,
    key4 => value4,
    ...
)
EXAMPLE 1 Simple Array


<?php
$array = array(
    "name" => "Nakiya",
    "age" => 25,
);

The key can either be an integer or a string. The value can be of any type.

EXAMPLE 2 Mixed integer and string keys

<?php
$array = array(
    "name" => "Nakiya",
    "age" => 25,
    200   => -50,
    -50  => 200,
);
var_dump($array);
?>
Above Example output 

array(4) { 
["name"]=> string(6) "Nakiya" 
["age"]=> int(25) 
[200]=> int(-50) 
[-50]=> int(200) 
}


Example 3 Accessing array elements

<?php
$array = array(
    "name" => "Nakiya",
      50    => 100,
    "address" => array(
         "country" => array(
             "city" => "New York"
         )
    )
);

var_dump($array["name"]);
var_dump($array[50]);
var_dump($array["address"]["country"]["city"]);
?>

Above example output

string(6) "Nakiya" 
int(100) 
string(8) "New York"


No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...