ecotechie
ecotechie

Reputation: 91

Append to string in existing associative array value in PHP

I want to append to an existing string inside an associative array.

I know this works:

$a = ['key' => 'value'];
$a['key'] = $a['key'] . ' appended';

But am wondering of other options...

Upvotes: 0

Views: 600

Answers (2)

Z0OM
Z0OM

Reputation: 960

I am of the opinion that you cannot do that in php.

You can't append/extend values to an existing array value in php!

All solutions are only an overwrite/replace of an existing value in an array.

Check this in php.net Arrays

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94662

Another option

$a = ['key' => 'value'];
$a['key'] .= ' appended';

Another option

$a['key'] = sprintf('%s %s' , $a['key'], 'appended');

Upvotes: 0

Related Questions