Adding to a multi-dimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Anarking
Forum Newbie
Posts: 24
Joined: Wed Feb 11, 2009 11:29 am

Adding to a multi-dimensional array

Post by Anarking »

Hi there


I have this multidimensional array thing

Code: Select all

 
$doc = array (
    1 => array ("Oliver", "Peter", "Paul"),
    2 => array ("Marlene", "Lucy", "Lina"),
    );
 
But I want to add another array to it later called like this

Code: Select all

 
for ($x = 0; $x < 5;$x++)
    {
     $rowone[$x] = $x;
    }
 
 
How do I add this new array to the multidimensional array properly?


Ive tried just adding in

Code: Select all

 
3 => ($rowone);
 
but that doesnt work
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Adding to a multi-dimensional array

Post by McInfo »

There should be no comma after the last element in an array.

Code: Select all

$doc = array (
    1 => array ("Oliver", "Peter", "Paul"),
    2 => array ("Marlene", "Lucy", "Lina"), //<--- bad comma
    );
The problem with the following code is the semicolon.

Code: Select all

3 => ($rowone); //<--- bad semicolon
This works:

Code: Select all

<?php
header('Content-Type: text/plain');
 
for ($x = 0; $x < 5; $x++) {
    $rowone[$x] = $x;
}
 
$doc = array
(   1 => array('Oliver', 'Peter', 'Paul')
,   2 => array('Marlene', 'Lucy', 'Lina')
,   3 => $rowone
);
 
print_r($doc);
?>
The output is:

Code: Select all

Array
(
    [1] => Array
        (
            [0] => Oliver
            [1] => Peter
            [2] => Paul
        )
    [2] => Array
        (
            [0] => Marlene
            [1] => Lucy
            [2] => Lina
        )
    [3] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
        )
)
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 2:32 pm, edited 1 time in total.
Anarking
Forum Newbie
Posts: 24
Joined: Wed Feb 11, 2009 11:29 am

Re: Adding to a multi-dimensional array

Post by Anarking »

ah, cheers buddy
Post Reply