1

I keep getting

Warning: Illegal string offset 'type' in ... on line ...

I've tried following the answers here Illegal string offset Warning PHP

by doing something like

if(isset($_POST['type_'.$i]))
     $$T['type'] = $_POST['type_'.$i];

but it still gives errors, I think it might have something to do with variable variables (it's the first time I am using them. Below is my code:

for($i = 1; $i <= 15; $i++){
    $T = 'T'.$i;

    $$T['type'] = $_POST['type_'.$i];
    $$T['hidden'] = $_POST['hidden_'.$i];
    $$T['require'] = $_POST['require_'.$i];
    if(isset($_POST['question_'.$i.'_list']))
        $$T['list'] = $_POST['quesiton_'.$i.'_list'];
}

I won't like to create arrays T1, T2 ... T15, with the following values ['type'], ['hidden'], ['require'], ['list'].

4

2 回答 2

3

怎么样?

for($i = 1; $i <= 15; $i++){
    $T = 'T'.$i;
    $$T = array(
      'type' => $_POST['type_'.$i], 
      'hidden' => $_POST['hidden_'.$i],
      'require' => $_POST['require_'.$i]);

    if(isset($_POST['question_'.$i.'_list']))
        ${$T}['list'] = $_POST['question_'.$i.'_list'];
}
于 2014-06-10T22:19:27.020 回答
2

问题是优先级之一。$T['type']首先解析,然后用作 的变量名$___

因为$T是一个字符串,['type']所以是一个无效的偏移量。

你可以这样做:

${$T}['type']

... 我认为。我真的不知道,因为像这样的东西就是数组的用途;)

$T = array();
for( $i = 1; $i <= 15; $i++) {
    $row = array(
        "type" => $_POST['type_'.$i],
        "hidden" => $_POST['hidden_'.$i],
        "require" => $_POST['require_'.$i]
    );
    if( isset($_POST['question_'.$i.'_list'])) {
        $row['question_'.$i.'_list'] = $_POST['question_'.$i.'_list'];
    }
    $T[] = $row;
}
于 2014-06-10T22:19:13.500 回答