Arrays
What This Tutorial Covers
Indexed Arrays
Numerically indexed lists of values.
Associative Arrays
Key-value pairs with named keys.
Array Functions
Sort, filter, map, and merge built-ins.
Noble Desktop's Full-Stack Web Development Certificate teaches modern back-end development — concepts that carry across PHP, Node.js, and Python.
Dive into the intricacies of PHP & MySQL with this comprehensive tutorial covering topics like creating a simple array, creating associative arrays using shorthand, and multidimensional arrays.
Below the bottom echo line, add the following bold code:
$restaurant = [ 'name' => 'Nobu', 'type' => 'Sushi', 'price' => 'Expensive' ];Here we have created a new array called
$restaurantand given it some values. Notice everything goes in between the brackets. The item on the left is called a key, and the element on the right of the => is its value.Let’s test outputting part of this array. Add the following bold code:
$restaurant = [ 'name' => 'Nobu', 'type' => 'Sushi', 'price' => 'Expensive' ]; echo $restaurant['name']. ' is very '.$restaurant['price'];Here we simply output the variables and again use the period to CONCATENATE them to our string.
Save the page and then in a browser go to (or reload):
- Mac: localhost:8888/phpclass/array.php
- Windows: localhost/phpclass/array.php
The page will read: Nobu is very Expensive