0

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.

1
  • Maybe you could add what you expect the result to look like...? As you say, it makes little sense as is. Commented Jan 29, 2014 at 16:36

1 Answer 1

1

I think that s what you are looking for

foreach ($arr as $key => &$value)
{
  $value[] = array('name' => 'test', 'age' => 28);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.