0

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

1
  • 1
    $arrayname[indexname] = $value; try using this Commented Aug 17, 2017 at 3:32

1 Answer 1

2

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 )

Sign up to request clarification or add additional context in comments.

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.