PHP能否使用变量变量来访问数组键?

huangapple go评论100阅读模式
英文:

PHP can i access array key with variable variables?

问题

  1. 我有一个包含一些信息的数组,但我尝试以可变变量的方式访问数组,但我不知道如何实现,因为PHP解释整个字符串是一个变量而不是带有关联键的变量。
  2. 我的示例代码:
  3. $myArrayWithData = ["info" => 1];
  4. $myvarvarArray = "myArrayWithData['info']";
  5. print_r($$myvarvarArray);

期望行为:

  1. 1

实际行为:

  1. Notice: Undefined variable: myArrayWithData['info']

我不想硬编码索引,因为我试图创建一个Twig函数,通过字符串创建数组索引路径,而我无法预测数组键。

  1. $myvarvarArray = "myArrayWithData";
  2. print_r($$myvarvarArray["info"]); #硬编码的索引

编辑:我的函数可以添加多个索引

  1. /**
  2. * 向您的Twig数组中添加一个值,而不是使用合并
  3. *
  4. * @param $object 要设置新值的数组或对象(公共访问)
  5. * @param string $context 要设置值的对象完整路径,例如 "children.grandchildren.info"
  6. * @param $value 要插入的值
  7. *
  8. */
  9. public function enhancedMerge($object, $contexts, $value){
  10. $contexts = explode('.', $contexts);
  11. $newObject = $object;
  12. $path = '';
  13. foreach($contexts as $key => $context){
  14. $objectWithPath = 'newObject' . $path;
  15. $contextIsArray = is_array(${$objectWithPath}) ? true : false;
  16. if($contextIsArray){
  17. $path .= "['" . $context . "']";
  18. }else{
  19. $path .= "->" . $context;
  20. }
  21. }
  22. $objectWithPath = $newObject . $path;
  23. $$objectWithPath = $value;
  24. return $$newObject;
  25. }
英文:

i have an array with some info but i am trying to implement a way to access array dynamically with variable variables but i have no idea how, because php interprets that the whole string is a variable and not a variable with an associative key.

my example code:

  1. $myArrayWithData = ["info" => 1];
  2. $myvarvarArray = "myArrayWithData['info']";
  3. print_r($$myvarvarArray);

expected behavior:

  1. 1

Actual Behavior:

  1. `"Notice: Undefined variable: myArrayWithData['info']"`

i don want to hardcode indexes because i am trying to create a twig function that creates the array index path by a string and i am not able to predict the array Keys.

  1. $myvarvarArray = "myArrayWithData"
  2. print_r($$myvarvarArray["info"]); #hardcoded index

edit: my function can add more than 1 index

  1. /**
  2. * add a value to your twig array instead of using merge
  3. *
  4. * @param $object Array or object(public access) to set a new value
  5. * @param string $context object complete path where value will be setted example "children.grandchildren.info"
  6. * @param $value value to be inserted
  7. *
  8. */
  9. public function enhancedMerge($object, $contexts, $value){
  10. $contexts = explode('.', $contexts);
  11. $newObject = $object;
  12. $path = '';
  13. foreach($contexts as $key => $context){
  14. $objectWithPath = 'newObject' . $path;
  15. $contextIsArray = is_array(${$objectWithPath}) ? true : false;
  16. if($contextIsArray){
  17. $path .= "['" . $context . "']";
  18. }else{
  19. $path .= "->" . $context;
  20. }
  21. }
  22. $objectWithPath = $newObject . $path;
  23. $$objectWithPath = $value;
  24. return $$newObject;
  25. }

答案1

得分: 1

不可以使用变量变量来访问数组键。

然而,我们可以使用引用变量来访问它们,就像这个答案中所示。

我的函数现在向数组"twigs"添加值

  1. public function enhancedMerge($object, $contexts, $value)
  2. {
  3. $contexts = explode('.', $contexts);
  4. $elemCount = count($contexts);
  5. $current = &$object;
  6. foreach ($contexts as $key => $context) {
  7. if (is_array($current)) {
  8. $current = &$current[$context];
  9. } elseif ($key != $elemCount-1) {
  10. $current = &$current->$context;
  11. }
  12. }
  13. $backup = $current;
  14. $current = $value; #设置新值并通过引用修改$object
  15. return $object;
  16. }
英文:

Can I access array key with variable variables?

Short answer: No.

However, we can access them using reference variables like this answer.

My function now adds values to array twigs

  1. public function enhancedMerge($object, $contexts, $value)
  2. {
  3. $contexts = explode('.', $contexts);
  4. $elemCount = count($contexts);
  5. $current = &$object;
  6. foreach ($contexts as $key => $context) {
  7. if (is_array($current)) {
  8. $current = &$current[$context];
  9. } elseif ($key != $elemCount-1) {
  10. $current = &$current->$context;
  11. }
  12. }
  13. $backup = $current;
  14. $current = $value; #set new value and modify $object by reference
  15. return $object;
  16. }

huangapple
  • 本文由 发表于 2023年4月4日 03:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定