PHP

Getting Started String Date If Else Loop Array Functions Session Files Super Global Variables Debugging MySQL MySQL Read Data JSON Markdown
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

PHP : Array

2017-01-01

Arrays

Basics

Create a new array and output the first value.

$numbers = array(4,7,15,16,23,42);
echo $numbers[0]; //outputs 4

Arrays can contain a mixture of elements including integers, strings, and other arrays.

$mixed = array (3, "Bear", "Wolf", array("a","b","c"));
echo $mixed[0]; // outputs 3
echo $mixed[3][1]; // outputs b
echo $mixed; // outputs an error 

Shortcut for creating array in PHP 5.4 and above.

$letters= ["R","O","B"];

Output

Use print_r to output array content

echo print_r($mixed)
//outputs Array ( [0] => 3 [1] => Bear [2] => Wolf [3] => Array ( [0] => a [1] => b [2] => c ) ) 

Put it in pre tags to get more readable array values

<pre><?=print_r($mixed)?></pre>
//outputs
Array
(
    [0] => 3
    [1] => Bear
    [2] => Wolf
    [3] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)

Add To An Array

This will override the current value in first position.

$mixed[0] = "Ketamine Moose";

This will add a new value to at the end.

$mixed[] = "horse";

Associative Arrays

Associative Arrays are key/value pairs.

Create a new array and output some values:

$assoc = array("first_name"=>"Rob", "last_name"=>"Powers");
echo "My name is " . $assoc["first_name"] . " " . $assoc["last_name"]; //output: My name is Rob Powers 

Instead of having an index for each value (like a regular array), it has a name for each value. The name can be a string or an int.

These 2 arrays are exactly the same:

$numbers = array(4,7,15,16,23,42);
$numbers = array(0 => 4, 1 => 7, 2 => 15, 3  => 16, 4  => 23, 5  => 42);

Array Functions

There are lots of prebuilt array functions. http://php.net/manual/en/ref.array.php

Sort Array

This will sort array in order by number or by alphabetical order.

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
echo print_r($fruits); //Outputs: Array ( [0] => apple [1] => banana [2] => lemon [3] => orange ) 

Check If Is Array

Returns true or false.

$numbers = array(1,2,3);
echo is_array($yes) ? 'Is array.' : 'Is not an Array.';

Check If Value Exists In Array

$numbers = array(4,7,15,16,23,42);
in_array(16, $numbers); // returns true

Check If Key Exists In Array

$food = array('mexican' => "burrito", 'italian' => "calzone");
array_key_exists('mexican', $food); // returns true

Get All Keys From Array

Outputs into an array with just the keys.

$assoc = array("first_name"=>"Rob", "last_name"=>"Powers");
print_r(array_keys($assoc)); //Output: Array ( [0] => first_name [1] => last_name ) 

Loop Through Array

Loop through each item in the array, getting the key and making it look nice.

$person = array(
    "first_name" => "Rob",
    "last_name" => "Powers",
    "address1" => "123 Main Street",
    "city" => "Beverly Hills",
    "state" => "CA",
    "zip" => "90210"
);
foreach ($person as $key => $value) {
    $key_nice = ucwords(str_replace("_", " ", $key));
    echo "{$key_nice}: {$value} <br>";
}
// Output:
// First Name: Rob
// Last Name: Powers
// Address1: 123 Main Street
// City: Beverly Hills
// State: CA
// Zip: 90210