指向数组或嵌套对象的变量

发布于 2024-08-17 12:24:43 字数 617 浏览 11 评论 0原文

是否可以创建一个指向数组或嵌套对象的变量? php 文档明确指出您不能指向 SuperGlobals,但不清楚(至少对我来说)这是否适用于一般数组。

这是我对数组 var var 的尝试。

     // Array Example
     $arrayTest = array('value0', 'value1');
     ${arrayVarTest} = 'arrayTest[1]';
     // This returns the correct 'value1'
     echo $arrayTest[1];
     // This returns null
     echo ${$arrayVarTest};   

下面是一些简单的代码来展示我所说的对象 var var 的含义。

     ${OBJVarVar} = 'classObj->obj'; 
     // This should return the values of $classObj->obj but it will return null  
     var_dump(${$OBJVarVar});    

我在这里遗漏了一些明显的东西吗?

Is it possible to create a variable variable pointing to an array or to nested objects? The php docs specifically say you cannot point to SuperGlobals but its unclear (to me at least) if this applies to arrays in general.

Here is my try at the array var var.

     // Array Example
     $arrayTest = array('value0', 'value1');
     ${arrayVarTest} = 'arrayTest[1]';
     // This returns the correct 'value1'
     echo $arrayTest[1];
     // This returns null
     echo ${$arrayVarTest};   

Here is some simple code to show what I mean by object var var.

     ${OBJVarVar} = 'classObj->obj'; 
     // This should return the values of $classObj->obj but it will return null  
     var_dump(${$OBJVarVar});    

Am I missing something obvious here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白馒头 2024-08-24 12:24:43

数组元素方法

  • 从字符串中提取数组名称并将其存储在$arrayName中。
  • 从字符串中提取数组索引并将其存储在$arrayIndex中。
  • 正确解析它们而不是整体解析它们。

代码:

$arrayTest  = array('value0', 'value1');
$variableArrayElement = 'arrayTest[1]';
$arrayName  = substr($variableArrayElement,0,strpos($variableArrayElement,'['));
$arrayIndex = preg_replace('/[^\d\s]/', '',$variableArrayElement);

// This returns the correct 'value1'
echo ${$arrayName}[$arrayIndex];

对象属性方法

  • 通过分隔符 (->) 分解包含要访问的类和属性的字符串。
  • 将这两个变量分配给 $class$property
  • var_dump() 上单独解析它们而不是整体解析它们

代码:

$variableObjectProperty = "classObj->obj";
list($class,$property)  = explode("->",$variableObjectProperty);

// This now return the values of $classObj->obj
var_dump(${$class}->{$property});    

它有效!

Array element approach:

  • Extract array name from the string and store it in $arrayName.
  • Extract array index from the string and store it in $arrayIndex.
  • Parse them correctly instead of as a whole.

The code:

$arrayTest  = array('value0', 'value1');
$variableArrayElement = 'arrayTest[1]';
$arrayName  = substr($variableArrayElement,0,strpos($variableArrayElement,'['));
$arrayIndex = preg_replace('/[^\d\s]/', '',$variableArrayElement);

// This returns the correct 'value1'
echo ${$arrayName}[$arrayIndex];

Object properties approach:

  • Explode the string containing the class and property you want to access by its delimiter (->).
  • Assign those two variables to $class and $property.
  • Parse them separately instead of as a whole on var_dump()

The code:

$variableObjectProperty = "classObj->obj";
list($class,$property)  = explode("->",$variableObjectProperty);

// This now return the values of $classObj->obj
var_dump(${$class}->{$property});    

It works!

中二柚 2024-08-24 12:24:43

使用 = & 通过引用分配:

 $arrayTest = array('value0', 'value1');
 $arrayVarTest = &$arrayTest[1];

 $arrayTest[1] = 'newvalue1'; // to test if it's really passed by reference

 print $arrayVarTest;

Use = & to assign by reference:

 $arrayTest = array('value0', 'value1');
 $arrayVarTest = &$arrayTest[1];

 $arrayTest[1] = 'newvalue1'; // to test if it's really passed by reference

 print $arrayVarTest;
我喜欢麦丽素 2024-08-24 12:24:43

echo $arrayTest[1]; 中,变量名称是 $arrayTest ,数组索引为 1,而不是 $arrayTest[ 1]。括号是 PHP“关键字”。与方法表示法和 -> 运算符相同。所以你们需要分开。

// bla[1]
$arr = 'bla';
$idx = 1;
echo $arr[$idx];

// foo->bar
$obj = 'foo';
$method = 'bar';
echo $obj->$method;

您想要做的事情听起来更像是评估 PHP 代码 (eval())。但请记住:评估是邪恶的。 ;-)

In echo $arrayTest[1]; the vars name is $arrayTest with an array index of 1, and not $arrayTest[1]. The brackets are PHP "keywords". Same with the method notation and the -> operator. So you'll need to split up.

// bla[1]
$arr = 'bla';
$idx = 1;
echo $arr[$idx];

// foo->bar
$obj = 'foo';
$method = 'bar';
echo $obj->$method;

What you want to do sounds more like evaluating PHP code (eval()). But remember: eval is evil. ;-)

心碎的声音 2024-08-24 12:24:43

不,你不能那样做。您只能使用变量、对象和函数名称来做到这一点。

示例:

 $objvar = 'classObj';
 var_dump(${$OBJVarVar}->var);

替代方案可以通过 eval() 或进行预处理。

$arrayTest = array('value0', 'value1');
$arrayVarTest = 'arrayTest[1]';

echo eval('return 

也就是说,如果您非常确定输入的内容是什么。

通过预处理:

function varvar($str){
  if(strpos($str,'->') !== false){
    $parts = explode('->',$str);
    global ${$parts[0]};
    return $parts[0]->$parts[1];
  }elseif(strpos($str,'[') !== false && strpos($str,']') !== false){
    $parts = explode('[',$str);
    global ${$parts[0]};
    $parts[1] = substr($parts[1],0,strlen($parts[1])-1);
    return ${$parts[0]}[$parts[1]];
  }else{
    return false;
  }
}

$arrayTest = array('value0', 'value1');
$test = 'arrayTest[1]';
echo varvar($test);
.$arrayVarTest.';'); eval('echo

也就是说,如果您非常确定输入的内容是什么。

通过预处理:


.$arrayVarTest.';');

也就是说,如果您非常确定输入的内容是什么。

通过预处理:

Nope you can't do that. You can only do that with variable, object and function names.

Example:

 $objvar = 'classObj';
 var_dump(${$OBJVarVar}->var);

Alternatives can be via eval() or by doing pre-processing.

$arrayTest = array('value0', 'value1');
$arrayVarTest = 'arrayTest[1]';

echo eval('return 

That is if you're very sure of what's going to be the input.

By pre-processing:

function varvar($str){
  if(strpos($str,'->') !== false){
    $parts = explode('->',$str);
    global ${$parts[0]};
    return $parts[0]->$parts[1];
  }elseif(strpos($str,'[') !== false && strpos($str,']') !== false){
    $parts = explode('[',$str);
    global ${$parts[0]};
    $parts[1] = substr($parts[1],0,strlen($parts[1])-1);
    return ${$parts[0]}[$parts[1]];
  }else{
    return false;
  }
}

$arrayTest = array('value0', 'value1');
$test = 'arrayTest[1]';
echo varvar($test);
.$arrayVarTest.';'); eval('echo

That is if you're very sure of what's going to be the input.

By pre-processing:


.$arrayVarTest.';');

That is if you're very sure of what's going to be the input.

By pre-processing:

一百个冬季 2024-08-24 12:24:43

对于许多嵌套级别有一种动态方法:

$attrs = ['level1', 'levelt', 'level3',...];
$finalAttr = $myObject;
foreach ($attrs as $attr) {
    $finalAttr = $finalAttr->$attr;
}
return $finalAttr;

there is a dynamic approach for to many nested levels:

$attrs = ['level1', 'levelt', 'level3',...];
$finalAttr = $myObject;
foreach ($attrs as $attr) {
    $finalAttr = $finalAttr->$attr;
}
return $finalAttr;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文