I try differently:

$var["{$dir}"]; $var[$dir]; $var[(string)$dir]; ... 

As soon as I do not try, I constantly get an error: Notice: Undefined index: значение in ...

And if you print the $dir variable itself, then the value is output. Fantasy!

I can not get value

  • The second option. but it gives an error because there is no such element in the array - Ipatyev
  • @ Ipatiev Is! Array ( [kylticket] => 2.2 ) - Vladimir
  • no. The first skill of a programmer is to learn to believe your eyes. and if pkhp says that there is no such element - it means NO - Ipatiev
  • one
    This is another mistake novice programmers. “I did this and that” - this is nothing at all. You can imagine anything, but reality tells you the opposite with a human voice. It is necessary not to bleat "I have circumcised" but to CHECK WHAT IS ARE IN THE VARIABLE. echo rawurlencode($dir."<br>".key($updates_result)); - Ipatiev
  • one
    Well, common sense is just important here. If the system says that there is no key, then it is the way it is, and we must not excuse ourselves, but look for the cause. <script> could show and just view the HTML source in the browser - Ipatyev

1 answer 1

How to insert into an array key from a variable in PHP?

As I understand it, you need to get a new array with the keys you know. If I do not understand correctly, then clarify the question and I will rewrite the answer, and if the solution is correct, here is the solution:

These actions

 $var["{$dir}"]; $var[$dir]; $var[(string)$dir]; 

You call an element of this array using the $ dir key. If you want to add a new key to the $ var array, then indicate to it any value acceptable to you using this key, for example:

 //$var["{$dir}"]=''; $var[$dir]='';//операция присвоения - мы присваиваем массиву $var //значение(пустое в данном случае - пустой символ) по ключу $dir //$var[(string)$dir]=''; print_r($var);