1

If I have a variable,

 $var1 = 'foo';      //but this value might change over time. 

How can I assign the value of $var1 to array $array1?

I tried

 $array1 = array('key1' => $var1;)

but this doesn't seems to work.

1
  • try $array1[] = $var1; Commented Nov 18, 2013 at 5:48

5 Answers 5

1

Try the following:

$array1['key1'] = $var1;

You can also do the following:

 $array1 = array( 'key1' => $var1);

But you need to remove ; from your code.

Sign up to request clarification or add additional context in comments.

Comments

0

Simple as this.. Just remove the ;

 <?php
 $var1 = 'foo';
 $array1 = array( 'key1' => $var1 );

Comments

0

you can use, $array1 = array('key1' => $var1);

single dimensional array structure:

array('key'=>'value', 'key2'=>'value' ...);

Comments

0

Try

$var1='foo';
    $array1 = array(
        'key1' => $var1
    );

OR

$array1['key1'] = $var1;

Comments

0

Try this, I think this will help you

    $var="abc";
      $array=array('name'=>$var);
       echo $array['name'];

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.