开发者

How do I generate an array from a string representation of that array?

开发者 https://www.devze.com 2022-12-22 12:05 出处:网络
I want to generate the array $result_array. There is no error at the page, but not works! that not works !

I want to generate the array $result_array. There is no error at the page, but not works!

that not works !

//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_arr开发者_高级运维ay .= '"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);

that works

$_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"]
);


Because you're trying to get PHP to treat a string as code. My question would be "why" - but if you MUST do it, you're looking for eval : http://php.net/manual/en/function.eval.php

// untested
$_RESULT = eval("return " . "array($result_array)" . ";");

Will probably give you the result you're loking for.

The real qusetion is why aren't you just doing this:

if (ACCOUNT_GENDER == 'true') {
 $result_array['gender_male'] = $male;
 $result_array['gender_female'] = $female;
}


PHP doesn't work that way. A string is not always equivalent to the actual thing you want to use.


The first example just creates an array with one really large string. The "=>"s are included in that string and are not interpreted to create a new array element. Look at the second example. Are the "=>"s there inside of quotes?


You already answered your own question. The first cannot work. You are just creating a long string and then stick that string as single element into an array.

But looking at your two examples I suppose you're wanting to do something like this:

$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;


You can create PHP array like this:

$array = array();

or

$array[] = 'whatever';

But in your code, you are using:

$result_array .= '"messages" => "' . $errors .'",';

Which is not how to create PHP array.


//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


Why are you trying to do this? Are you storing the array in text somewhere? If it doesn't need to be human readable, you should look at:

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);

0

精彩评论

暂无评论...
验证码 换一张
取 消