Arrays

Populate (fill) an index array with numbers

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

Populate an index 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] => Frances Kafka
            [published] => 1925
        )

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

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

)
 

CODE FOLLOWS

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

// code
error_reporting(E_ALL);    // set error reporting to all

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

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

// populate array
$myarray = array();

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

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

// show contents of array
print_r($myarray);

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


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

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

// show contents of array
echo('<pre>');
print_r($myarray);
echo(
'</pre>');

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

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

// show contents of array
echo('<pre>');
print_r($myarray);
echo(
'</pre>');

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

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

// show contents of array
echo('<pre>');
print_r($myarray);
echo(
'</pre>');

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