ahmed_omar
ahmed_omar

Reputation: 49

how to pass string into array as key and value

trying to pass string key and value into my array , here's the array

$array = array(
   'id' =>1,
   'name' => 'ahmed',
   'age' => 16,
   'country' => 'egypt',
   $string,
);

and my string var is

$string = "  'city'=>'cairo'  ";

when using the code in that way doesn't works so is there anyway to inserting string in a array as key and value

Upvotes: 0

Views: 100

Answers (1)

Rey Norbert Besmonte
Rey Norbert Besmonte

Reputation: 791

Try this one

$array = array(
       'id' =>1,
       'name' => 'ahmed',
       'age' => 16,
       'country' => 'egypt',
    );
         $array['city'] = 'cairo';
         print_r( $array);

it will output

Array ( [id] => 1 [name] => ahmed [age] => 16 [country] => egypt [city] => cairo )

Upvotes: 2

Related Questions