Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to assign variable values to keys while making a multidimensional array? For example:

//creates array
$arr = array(
    'First' => array(),
    'Second' => array()
);

//assigns strings to variables
$name = 'First';
$once = 'Name';
$twice = 'Age';
$thrice = 'Whatever';

//loops twice to create arrays
do {
    $arr[$name][] = $once => array(), $twice => array(), $thrice => array();
    $once = 'Another';
    $twice = 'Example';
    $thrice = 'You get the point';
    if ($name == 'First') {
        $name = 'Second';
    } else {
        $name = 'end';
    }
} while ($name == 'Second');

The above example hardly makes sense to a living thing, let alone a piece of metal on my desk. If the above cannot be done with variables, how else could I create a multidimensional array with keys without explicitly coding it all out? Thanks in advance.

EDIT: To clarify, here's a sample call to the array I'd like to make:

echo $arr['Second']['Another'][2];

The third dimensional part of the array would be assigned with a loop not included, i.e., the 2. I'm having difficulty creating the second dimension is all.

share|improve this question
    
Maybe you could add what you expect the result to look like...? As you say, it makes little sense as is. –  deceze Jan 29 at 16:36

1 Answer 1

I think that s what you are looking for

foreach ($arr as $key => &$value)
{
  $value[] = array('name' => 'test', 'age' => 28);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.