0

i want to set one key's value to another key's value + some string

i tried this

    $b = array ( 
      'name'        => 'Gilbert',
      'fullname '   => $b['name']. 'Hocvinger',
       );

echo $b['fullname'];

but it gives me an error.

1
  • It doesn't work like this, because the array has not been defined yet. You'd need to define the name outside of the array decleration, and then you can add it to the array. Commented Nov 8, 2013 at 13:33

2 Answers 2

4

you can do this after initializing the array

$b = array ( 
      'name' => 'Gilbert'
);

$b['fullname ']  = $b['name']. 'Hocvinger';

echo $b['fullname'];
Sign up to request clarification or add additional context in comments.

Comments

0

You can not act like this way since during definition of array it's fields are inaccessible because array is not defined yet.

But you can easily do that after array's definition, like:

$b = array('name' => 'Gilbert');
$b['fullname'] = $b['name'].' Hocvinger';

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.