2

I'd like to generate arrays with the name of the values in another array like this

$array_names = array("name1","name2","name3",...);

So, for each item inside of my $array_names. I'd like to generate an array with the name of the items like this

$name1, $name2, $name3 ... etc...

I tried something like

foreach( $array_names as $name){
    $name = array();
//or like this:
    $."$name" = array();
}

but clearly, I am kinda lost... Any idea?

1
  • 2
    Bad idea. Just use a multidimensional array. But there is extract(). Commented Jun 8, 2015 at 17:27

2 Answers 2

1

You're close :)

foreach($array_names as $name) $$name = array();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Adelphia (Y)
1

Try this:

$array_names = array("name1","name2","name3");

foreach( $array_names as $name){
   $$name = array();
}

To test:

echo "<pre>";
print_r(get_defined_vars())

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.