doulu8847 2019-06-23 09:16
浏览 55

php通过其键访问多维数组的特定位置并进行设置

In PHP we can do things like these:

Class Example {
 ...
}
$example = 'Example';
$object = new $example();

Or the use of variable variables:

$hour = 18;
$greets = array('Good morning','Good afternoon','Good evening');
$values = array(13,21,23);//people is sleeping at 23PM, so they don't greet.
$n = count($values);
$greet = 'greets';
for($i=0;$i<$n;$i++){
   if($hour < $values[$i]){
       echo 'hello, '.${$greet}[$i];
       break;
   }
}

And others..

I wonder if it would be possible to access directly to a specific index of a multidimensional array in a similar way. Something like:

$array = array(...); //multidimensional array.
$position = '[0][4][3]';
print_r($array$position);

Thanks in advance.


UPDATE

I'm so sorry because I finished my question in a wrong way.

I need to set the multimesional array and add a value. i.e:

$array$position = $data;
  • 写回答

3条回答 默认 最新

  • donglv9116 2019-06-23 09:28
    关注

    Nope, this is not possible. The only thing you can do is to implement ArrayAccess interface, which allows to access instances with [] operator. But you will have to define the logic yourself.

    class MyClass implements ArrayAccess
    {
    ...
    }
    
    $x = new MyClass([0=>[4=>[3=>'hello world']]]);
    $position = '[0][4][3]';
    echo $x[$position]; //hello world
    
    评论

报告相同问题?

悬赏问题

  • ¥15 html5+css有人可以帮吗?
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?