Document

Arrays

Populate (fill) an indexed array with numbers

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => Steph
    [10] => 1
    [11] => 1.5
)

Populate an indexed array with days of the week

Array
(
    [0] => Sun
    [1] => Mon
    [2] => Tue
    [3] => Wed
    [4] => Thu
    [5] => Fri
    [6] => Sat
)

Populate an associative array with book data

Array
(
    [title] => The Grapes of Wrath
    [author] => John Steinbeck
    [published] => 1939
)

Populate a multidimensional associative array with book data

Array
(
    [0] => Array
        (
            [title] => The Grapes of Wrath
            [author] => John Steinbeck
            [published] => 1939
        )

    [1] => Array
        (
            [title] => The Trial
            [author] => Franz Kafka
            [published] => 1925
        )

    [2] => Array
        (
            [title] => The Hobbit
            [author] => J. R. R. Tolkien
            [published] => 1937
        )

    [3] => Array
        (
            [title] => A Tale of Two Cities
            [author] => Charles Dickens
            [published] => 1859
        )

)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Arrays</title>
    <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
</head>
<body>

<?php
include('../header/assignmentHeader.php');

error_reporting(E_ALL);

echo(
'<h1>Arrays</h1>');

echo(
'<p>Populate (fill) an indexed array with numbers</p>');

$myarray = array();

for (
$i = 1; $i < 10; $i++) {
    
$myarray[] = $i;
}

$myarray[] = "Steph";
$myarray[] = 1;
$myarray[] = 1.5;

echo(
'<pre>');
print_r($myarray);
echo(
'</pre>');


echo(
'<p>Populate an indexed array with days of the week</p>');

$myarray = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

echo(
'<pre>');
print_r($myarray);
echo(
'</pre>');


echo(
'<p>Populate an associative array with book data</p>');

$myarray = array(
    
'title' => "The Grapes of Wrath",
    
'author' => "John Steinbeck",
    
'published' => "1939"
);

echo(
'<pre>');
print_r($myarray);
echo(
'</pre>');


echo(
'<p>Populate a multidimensional associative array with book data</p>');

$myarray = array(
    array(
        
'title' => "The Grapes of Wrath",
        
'author' => "John Steinbeck",
        
'published' => "1939"
    
),
    array(
        
'title' => "The Trial",
        
'author' => "Franz Kafka",
        
'published' => "1925"
    
),
    array(
        
'title' => "The Hobbit",
        
'author' => "J. R. R. Tolkien",
        
'published' => "1937"
    
),
    array(
        
'title' => "A Tale of Two Cities",
        
'author' => "Charles Dickens",
        
'published' => "1859"
    
)
);

echo(
'<pre>');
print_r($myarray);
echo(
'</pre>');

show_source(__FILE__);

include(
'../footer/footer.php');
?>

</body>
</html>