1

Here's some code that I have trouble with. I don't know why, I feel I have used this code many times without any problems.

$people['firstname'] = "Fred";
$t = "firstname";
echo $people[$t] ;

echo returns nothing, whereas i expect it to return Fred.

Thanks for your help, Marc

7
  • 2
    There is nothing wrong with your code at all. Double check grammar, spelling and character case. Commented Feb 24, 2011 at 14:17
  • Cannot reproduce your issue. Pasted your code verbatim in a PHP 5.3 project. Works flawlessly. Commented Feb 24, 2011 at 14:18
  • Thanks for confirming the code is ok. Just have to find now why it doesn't work on my page. Commented Feb 24, 2011 at 14:20
  • 2
    @Marco I have a similar problem. Did you resolve yours? If yes, can you tell what the problem was? Commented Sep 14, 2014 at 21:48
  • I have same problem the array return noting when I use variable $CCodes2=$CCodes[$country]; but when i use echo $CCodes["LY"]; gives me correct value here is the array $CCodes=array("BH"=>"BHD","DZ"=>"DZD","EG"=>"EGP","IQ"=>"IQD","JO"=>"JOD","KW"=>"KWD","LB"=>"LBP","LY"=>"LYD", "MA"=>"MAD","OM"=>"OMR","QA"=>"QAR","SA"=>"SAR","SD"=>"SDG","TN"=>"TND","YE"=>"YER","MR"=>"MRO"); Commented Jan 18, 2015 at 11:41

4 Answers 4

2

Not sure why this isn't working for you.

$people['firstname'] = 'testvalue';

$key = 'firstname';

$value = $people[$key];

echo $value;

Works as expected, echos out "testvalue"

Double check your spelling and be consistent with your ticks (purely stylistic, I'm sure.)

Sign up to request clarification or add additional context in comments.

Comments

1

I think you can pass associative array value as variable . this is working for me

@$username=$_POST['username'];
@$password=$_POST['password'];

$result=array(
'username'=> "".$username."",
'password'=> "".$password.""
);

Comments

1

OK I found solution for my code, looks like variable contains, none ASCII character so I had to remove them, to make it works.

$country = preg_replace('/[[:^print:]]/', '', $country);
$CCodes2=$CCodes[$country];

You should check your php file encoding, e.g if you are use WYSIWYG editor or formatted text, make sue remove any of these none ASCII text before paste it .

2 Comments

this solved my issue
OG, my English were horrible back than, I fix some mistakes I typed.
0
$params_to_json = '{"' . str_replace(['=', '&'], ['":"', '","'], $_SERVER['QUERY_STRING']) . '"}';
$json_to_array = json_decode($params_to_json, true) ?? array(); 
extract($json_to_string); // Converts array keys into variable names and array values into variable value

As a function:

function url_params()
{
    $params_to_json = '{"' . str_replace(['=', '&'], ['":"', '","'], $_SERVER['QUERY_STRING']) . '"}';
    return json_decode($params_to_json, true) ?? array();
}
extract(url_params());

For example URL: example.com?id=3

echo $id //3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.