ahmed_omar ahmed_omar - 2 months ago 17
PHP Question

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

Answer Source

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 )