Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将字符串作为键和值传递到数组中_Php_Arrays_String - Fatal编程技术网

Php 如何将字符串作为键和值传递到数组中

Php 如何将字符串作为键和值传递到数组中,php,arrays,string,Php,Arrays,String,试图将字符串键和值传递到我的数组中,下面是数组 $array = array( 'id' =>1, 'name' => 'ahmed', 'age' => 16, 'country' => 'egypt', $string, ); 我的字符串var是 $string = " 'city'=>'cairo' "; 如果以这种方式使用代码不起作用,那么是否仍可以在数组中插入字符串作为键和值呢 $array = array(

试图将字符串键和值传递到我的数组中,下面是数组

$array = array(
   'id' =>1,
   'name' => 'ahmed',
   'age' => 16,
   'country' => 'egypt',
   $string,
);
我的字符串var是

$string = "  'city'=>'cairo'  ";
如果以这种方式使用代码不起作用,那么是否仍可以在数组中插入字符串作为键和值呢

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

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

试试这个

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

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


$arrayname[indexname]=$value;尝试使用此$arrayname[indexname]=$value;试试这个