Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Liquid Web Cloud VPS Hosting

10,567 questions

14,363 answers

573 users

How to create a two-dimensional array in PHP

4 Answers

0 votes
$arr = array(
    array(0, 1, 2),
    array(3, 4, 5),
    array(6, 7, 8),
);

echo "<pre>";
print_r($arr);
echo "</pre>";
 

 
/*
run:
      
Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
        )

    [2] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
        )

)
     
*/

 




answered Mar 7 by avibootz
0 votes
function create_2d_array($row, $col, $array_value = 0) {
    return array_fill(0, $row, array_fill(0, $col, $array_value));
}

$arr = create_2d_array(3, 5, 99);

echo "<pre>";
print_r($arr);
echo "</pre>";
 

 
/*
run:

Array
(
    [0] => Array
        (
            [0] => 99
            [1] => 99
            [2] => 99
            [3] => 99
            [4] => 99
        )

    [1] => Array
        (
            [0] => 99
            [1] => 99
            [2] => 99
            [3] => 99
            [4] => 99
        )

    [2] => Array
        (
            [0] => 99
            [1] => 99
            [2] => 99
            [3] => 99
            [4] => 99
        )

)
   
*/

 




answered Mar 7 by avibootz
0 votes
$arr = array(
    0 => array(
        'aaa' => 'PHP',
        'bbb' => 2453
    ),
    1 => array(
        'aaa' => 'C++',
        'bbb' => 8723
    ),
    2 => array(
        'aaa' => 'Python',
        'bbb' => 7862
    ),
);

echo "<pre>";
print_r($arr);
echo "</pre>";
 

 
/*
run:

Array
(
    [0] => Array
        (
            [aaa] => PHP
            [bbb] => 2453
        )

    [1] => Array
        (
            [aaa] => C++
            [bbb] => 8723
        )

    [2] => Array
        (
            [aaa] => Python
            [bbb] => 7862
        )

)
   
*/

 




answered Mar 7 by avibootz
0 votes
$arr = array();

$arr[0] = array();
$arr[0]['aaa'] = 'PHP';
$arr[0]['bbb'] = 34;

$arr[1] = array();
$arr[1]['aaa'] = 'C';
$arr[1]['bbb'] = 98;

$arr[2] = array();
$arr[2]['aaa'] = 'Java';
$arr[2]['bbb'] = 86;

echo "<pre>";
print_r($arr);
echo "</pre>";
 
 
/*
run:

Array
(
    [0] => Array
        (
            [aaa] => PHP
            [bbb] => 34
        )

    [1] => Array
        (
            [aaa] => C
            [bbb] => 98
        )

    [2] => Array
        (
            [aaa] => Java
            [bbb] => 86
        )

)
   
*/

 




answered Mar 7 by avibootz
...