T.Todua
T.Todua

Reputation: 56341

How to set IN AN ARRAY a key's value, using another key

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.

Upvotes: 0

Views: 41

Answers (2)

Alma Do
Alma Do

Reputation: 37365

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';

Upvotes: 0

dognose
dognose

Reputation: 20889

you can do this after initializing the array

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

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

echo $b['fullname'];

Upvotes: 4

Related Questions