简体   繁体   English

PHP 7.2 不能在 eval 中使用字符串偏移作为数组

[英]PHP 7.2 Cannot use string offset as an array in eval

The code was working in php 7.0.该代码在 php 7.0 中运行。 I am getting error after moved to 7.2 version.移至 7.2 版本后出现错误。 I need help in understand this.我需要帮助来理解这一点。 I have an $map and $tempArray print_r value of $locMap is given below我有一个 $map 和 $tempArray print_r 的 $locMap 值如下所示

Array
    (
        [1] => Array
            (
                [ID] => 1
                [LEVEL] => 1
                [NAME] => Operations1
                [CLASS] => ["e2"]["00"]

            )

        [2] => Array
            (
                [ID] => 2
                [LEVEL] => 1
                [NAME] => POperations2
                [CLASS] => ["e2"]["01"]

            )

        [26] => Array
            (
                [ID] => 26
                [LEVEL] => 2
                [NAME] => Operations3
                [CLASS] => ["e2"]["01"]["e2"]["00"]

            )
    )

print_r value of $tempArray is as below $tempArray 的 print_r 值如下

Array
    (
        [account] => Array
            (
                [0] => Array
                    (
                        [id] => 1000
                        [name] => Homes
                        [level] => 
                        [rno] => 9
                    )

            )

    )

Looping this $map as below循环这个 $map 如下

foreach ($map $rec=>$det) {
     $dummyArray = array();
     $dummyArray['id'] = $det['ID'];
     $dummyArray['CLASS'] = $det['CLASS'];
     eval("\$tempArray['account'][0]".$det["CLASS"]." = \$dummyArray;");
}

Error: Cannot use string offset as an array in *************: eval()'d code:1错误:不能在 ************* 中使用字符串偏移作为数组:eval()'d code:1

Inside eval function, "\$tempArray['account'][0]" is a string and is concatenating with the string again.在 eval function 内部,"\$tempArray['account'][0]" 是一个字符串,并再次与该字符串连接。 Why do I get the error in this case?为什么在这种情况下会出现错误? Help me understand this and how do I fix this issue?帮助我理解这一点以及如何解决此问题?

I believe the problem comes your concatenation of "\$tempArray['account'][0]".$det["CLASS"]." = \$dummyArray;"我相信问题出在你的串联"\$tempArray['account'][0]".$det["CLASS"]." = \$dummyArray;" . . If you output the string, you can see what is evaluated.如果你 output 字符串,你可以看到评估了什么。 Most probably, you forgot [] around $det["CLASS"].很可能,您忘记了 $det["CLASS"] 周围的 []。 In your case, the first part might be a string, not an array.在您的情况下,第一部分可能是字符串,而不是数组。

By the way, I am not sure to understand what you want to do but I am not sure you use the easiest way.顺便说一句,我不确定你想做什么,但我不确定你是否使用最简单的方法。

I do not understand why it worked with php7.0.我不明白为什么它适用于 php7.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM