我是PHP新手并使用php5.2,我需要将文件内容转换为二维数组,即我的文件内容将是
"Higher Studies" => "#",
"Symposiums" => "#",
"Conferences" => "#"
我的预期输出是,
Array ( [Higher Studies] => # [Symposiums] => # [Conferences] => # )
PHP函数我试图实现这一点,
$values = file_get_contents($url);
echo $values;
$array = explode(",", $values);
$array = array(file_get_contents($url));
$array = file('http://localhost/test1.php');
最后我得到了这样的答案,
Array ( [0] => "Higher Studies" => "#", [1] => "Symposiums" => "#", [2] => "Conferences" => "#" )
这是我们从文件中读取的可能方式还是可以像我期望的那样获得解决方案?
答案 0 :(得分:2)
你应该通过文件阅读它,然后去每一行,拆分并存储在数组
中$array = array();
$values = file($url);
foreach ($values as $idx => $row)
{
list($k, $v) = explode(' => ', $row);
$k = trim($k, '"');
$v = trim($v, '",');
$array[$k] = $v;
}
print_r($array);
或者你可以这样做:
$values = file_get_contents($url);
eval('$array = array('.$values.');');
print_r($array);