Skip to content

Instantly share code, notes, and snippets.

@zgulde
Last active February 26, 2016 21:45
Show Gist options
  • Select an option

  • Save zgulde/b96017a8b2540889dca3 to your computer and use it in GitHub Desktop.

Select an option

Save zgulde/b96017a8b2540889dca3 to your computer and use it in GitHub Desktop.

5.3.2 Associative Arrays

We can compare associative arrays to the objects we used in JavaScript. PHP also has objects, but they are a little different and we can compare the nesting of values in Javascript objects to nesting values in php associative arrays.

Javascript objects and PHP associative arrays both contain key value pairs.

JavaScript

{
    "name": "codeup",
    "bootcamp": "full stack"
}

PHP

[
    'name' => 'codeup',
    'bootcamp' => 'full stack'
]

In both of these examples the keys are the strings name and bootcamp and the values are the strings codeup and bootcamp.

Weather Data Example

Here we have a simplified representation of the weather forecast data response we got from the openweathermap api.

var forecast = {
    "city": "San Antonio",
    "cnt": 3,
    "days": [
        {
            "hi": 83,
            "low": 66,
            "conditions": "cloudy"
        },
        {
            "hi": 86,
            "low": 68,
            "conditions": "clear"
        },
        {
            "hi": 72,
            "low": 44,
            "conditions": "rain"
        }
    ],
    "location": {
        "lat": "29.4",
        "lon": "-98.5"
    },
    "id": "123456",
    "units": "imperial"
}

We could represent the same data with a php associative array:

$forecast = [
    'city' => 'San Antonio',
    'cnt' => 3,
    'days' => [
        [
            'hi' => 83,
            'low' => 66,
            'conditions' => 'cloudy'
        ],
        [
            'hi' => 86,
            'low' => 68,
            'conditions' => 'clear'
        ],
        [
            'hi' => 72,
            'low' => 44,
            'conditions' => 'rain'
        ]
    ],
    'location' => [
        'lat' => '29.4',
        'lon' => '-98.5'
    ],
    'id' => '123456',
    'units' => 'imperial'
]

To log the latitude of the forecast response in Javascript we would write

console.log(forecast.location.lat);

To get this same value from our php array we would write

echo $forecast['location']['lat'];

Extra Exercises

In php

  1. How would you echo the city name?
  2. How would you echo the the conditions of the first day? The third day?

Assignment By Refrence

We know we can assign a value to a variable, e.g.

$myString = 'a string';
$myNumber = 3;

We can also assign a variable to refrence the same value as another variable. If we change one variable, we then also change the other.

$a = 5;
echo $a; // 5
$b = &$a;
echo $b // 5
$b = 8;
echo $a; // 8
$a = 2;
echo $b; // 2

What will this code output?

$x = 'hello';
$y = &$x;
$y = $x . ' world';
$x = "{$y}!";
echo $y;

More operators

For the most part, PHP uses the same syntax for its operators as Javascript, so you are already familiar with most of it.

Remember, if you are unclear on order of operations, or just to make your code more readable, wrap expressions in parenthesis to make the order of operations explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment