views:

111

answers:

7

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

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"]
);
A: 

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

Ignacio Vazquez-Abrams
A: 

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?

Jonathan
+4  A: 

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("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;
}
Erik
the array is still not working. $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'] .',';.................................$_RESULT = eval("array($result_array)");
question_about_the_problem
@question_about_the_problem: You should not do it with eval. Use the other way Erik proposes. Besides being the only right way it is muuuuch easier to read.
Felix Kling
because when you are creating your string you aren't quoting the values and you need to. For example, this would be correct:`$result_array .= "'country' => '{$entry['entry_country_id']}',";` Notice the value is quoted.
Erik
@Felix: Thanks for suggestion. I've used second way. That's true "it is muuuuch easier to read"@Eric: This solutions is better than hurikhan77's solution. I have an another question:How i can create array as this:$array("products_image_sm_1" => "", products_image_xl_1" => "" ......... etc);I want to create that without eval and in the for loop.for an example:$parameters_images=array();for ($f=1; $f<=5; $f++) { $parameters_images = array_merge($parameters_images, array("products_image_sm_$f=>"" )); $parameters_images = array_merge($parameters_images, array("products_image_xl_$f=>"" ));}
question_about_the_problem
@question_about_the_problem: It would just be: `$parameters_images=array(); for ($f=1; $f<=5; $f++) { $parameters_images["products_image_sm_$f"] = ''; $parameters_images["products_image_xl_$f"] = '';}` I think you should read the basics about arrays: http://php.net/manual/en/language.types.array.php
Felix Kling
@Felix: Thanks, but I know the basicly informations about arrays. I just want to use best way.
question_about_the_problem
A: 

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;
hurikhan77
Thanks, it's good idea. This solution is working.
question_about_the_problem
A: 

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.

Sarfraz
yes, because I want to gererate the array. I know how i can create manual array.
question_about_the_problem
A: 
//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
Jayrox
A: 

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

mmattax