字符串式數組和數組式字符串

var_export — 輸出或返回一個變量的字符串表示php

eval — 把字符串做爲PHP代碼執行數組

 

//定義一個數組
$a=array(
	   'test'=>'tesq0',
		1=>'tesq1',
		2=>'tesq2',
		3=>'tesq3',
		4=>'tesq4',
		5=>'tesq5',	
);
數組轉換爲字符串
$x=var_export($a,true);
var_dump($x);
/* 
string(110) "array (
  'test' => 'tesq0',
  1 => 'tesq1',
  2 => 'tesq2',
  3 => 'tesq3',
  4 => 'tesq4',
  5 => 'tesq5',
)"
 */
字符串轉換爲數組

@eval("\$array = $x;"); 
var_dump($array);

/* 
array(6) {
  ["test"]=>
  string(5) "tesq0"
  [1]=>
  string(5) "tesq1"
  [2]=>
  string(5) "tesq2"
  [3]=>
  string(5) "tesq3"
  [4]=>
  string(5) "tesq4"
  [5]=>
  string(5) "tesq5"
}
*/
 
相關文章
相關標籤/搜索