PHP:致命错误-字符串中不支持运算符

时间:2019-04-17 11:30:59

标签: php

我有下面的代码,我得到的错误是

PHP: Fatal error -  operator not supported for strings in...

我已经阅读了一些文章和问题,但是看不到为什么这个特定的例子失败了。我看不到我将数组声明为字符串。

有什么想法吗?

代码

$a = Array();
foreach ($value as $k => $v) {

    $name = $v["name"];
    $value = $v["value"];

    $a["day_a"] = ($name == "day_a" ? 'true':'false');
    $a["day_b"] = ($name == "day_b" ? 'true':'false');
    $a["day_c"] = ($name == "day_c" ? 'true':'false');
    $a["day_d"] = ($name == "day_d" ? 'true':'false');
    $a["day_e"] = ($name == "day_e" ? 'true':'false');
    $a["day_f"] = ($name == "day_f" ? 'true':'false');

    if (strpos($name, '_radio_') !== false) {
        $string = substr($name,0,5);
        $a[$string] = $value;
    }

    if (strpos($name, '_select_') !== false) {
        $string = substr($name,0,5);
        $split = explode("_",$name);
        $type = $split[3];

        $a[$string][] = Array("type"=>$type,"location"=>$location); // ERROR HERE
    }

    // if name like day_a radio then $day_a is true
    // if $day_a

}

var_dump($ a,$ string); die();

array(6) {
  ["day_a"]=>
  string(5) "false"
  ["day_b"]=>
  string(5) "false"
  ["day_c"]=>
  string(5) "false"
  ["day_d"]=>
  string(5) "false"
  ["day_e"]=>
  string(5) "false"
  ["day_f"]=>
  string(5) "false"
}
string(5) "day_c"

1 个答案:

答案 0 :(得分:2)

您可能在$a[$string] = $value;上创建了字符串,然后尝试将数组追加到$a[$string][] = Array(...);

您可以添加支票:

if ( isset($a[$string]) ) {
    throw new Exception("Item ".$string." already exists!");
}