我想生成数组$ result_array。页面上没有错误,但不起作用!
//BOF: Result Array
$result_array = '';
$result_array .= '"messages" => "' . $errors .'",';
$result_array .= '"this_addr_type" => "' . (int)$_REQUEST['edit'] .'",';
if (ACCOUNT_GENDER == 'true') {
$result_array .= '"gender_male" => "' . $male .'",';
$result_array .= '"gender_female" => "' . $female .'",';
}
$result_array .= '"firstname" => "' . $entry['entry_firstname'] .'",';
$result_array .= '"lastname" => "' . $entry['entry_lastname'] .'",';
if (ACCOUNT_COMPANY == 'true') {
$result_array .= '"company" => "' . $entry['entry_company'] .'",';
}
$result_array .= '"street_address" => "' . $entry['entry_street_address'] .'",';
if (ACCOUNT_SUBURB == 'true') {
$result_array .= '"suburb" => "' . $entry['entry_suburb'] .'",';
}
$result_array .= '"postcode" => "' . $entry['entry_postcode'] .'",';
$result_array .= '"city" => "' . $entry['entry_city'] .'",';
if (ACCOUNT_STATE == 'true') {
$result_array .= '"state" => "' . $entry['entry_state'] .'",';
}
$result_array .= '"country" => "' . $entry['entry_country_id'] .'"';
//EOF: Result Array
$_RESULT = array($result_array);
$_RESULT = array(
"this_addr_type" => (int)$_REQUEST['edit'],
"gender_male" => $male,
"gender_female" => $female,
"firstname" => $entry["entry_firstname"],
"lastname" => $entry["entry_lastname"],
"company" => $entry["entry_company"],
"street_address" => $entry["entry_street_address"],
"suburb" => $entry["entry_suburb"],
"postcode" => $entry["entry_postcode"],
"city" => $entry["entry_city"],
"state" => $entry["entry_state"],
"country" => $entry["entry_country_id"]
);
答案 0 :(得分:4)
因为您正在尝试让PHP将字符串视为代码。我的问题是“为什么” - 但如果你必须这样做,你就是在寻找eval:http://php.net/manual/en/function.eval.php
// untested
$_RESULT = eval("return " . "array($result_array)" . ";");
可能会给你你想要的结果。
真正的问题是你为什么不这样做:
if (ACCOUNT_GENDER == 'true') {
$result_array['gender_male'] = $male;
$result_array['gender_female'] = $female;
}
答案 1 :(得分:0)
PHP无法正常工作。字符串并不总是等同于您想要使用的实际内容。
答案 2 :(得分:0)
第一个例子只是创建一个包含一个非常大的字符串的数组。 “=>”包含在该字符串中,不会被解释为创建新的数组元素。看看第二个例子。引号内是否有“=>”?
答案 3 :(得分:0)
您已经回答了自己的问题。第一个不能工作。您只是创建一个长字符串,然后将该字符串作为单个元素粘贴到数组中。
但是看看你的两个例子,我想你想要做这样的事情:
$result_array = array();
if($somecondition) {
$result_array = array_merge($result_array, array("entry1" => "data1", "entry2" => "data2"));
}
if($someothercondition) {
$result_array = array_merge($result_array, array("other_entry" => "more_data"));
}
$_RESULT = $result_array;
答案 4 :(得分:0)
您可以像这样创建PHP数组:
$array = array();
或
$array[] = 'whatever';
但是在您的代码中,您正在使用:
$result_array .= '"messages" => "' . $errors .'",';
这不是如何创建PHP数组。
答案 5 :(得分:0)
//BOF: Result Array
$_RESULT = array();
$_RESULT["messages"] = $errors;
$_RESULT["this_addr_type"] = (int)$_REQUEST['edit'];
if (ACCOUNT_GENDER == 'true') {
$_RESULT["gender_male"] = $male;
$_RESULT["gender_female"] = $female;
}
$_RESULT["firstname"] = $entry['entry_firstname'];
$_RESULT["lastname"] = $entry['entry_lastname'];
if (ACCOUNT_COMPANY == 'true') {
$_RESULT["company"] = $entry['entry_company'];
}
$_RESULT["street_address"] = $entry['entry_street_address'];
if (ACCOUNT_SUBURB == 'true') {
$_RESULT["suburb"] = $entry['entry_suburb'];
}
$_RESULT["postcode"] = $entry['entry_postcode'];
$_RESULT["city"] = $entry['entry_city'];
if (ACCOUNT_STATE == 'true') {
$_RESULT["state"] = $entry['entry_state'];
}
$_RESULT["country"] = $entry['entry_country_id'];
//EOF: Result Array
答案 6 :(得分:0)
你为什么要这样做?你是在某个地方将文件存储在文本中吗?如果它不需要人类可读,你应该看看:
http://php.net/manual/en/function.serialize.php
http://php.net/manual/en/function.unserialize.php
// $_RESULT as a string
$str = serialize($_RESULT);
// Back as an array
$arr = unserialize($str);