“不能将字符串偏移量用作数组”时,按键引用数组元素


"Cannot use string offset as an array" when referencing an array element by key

>我正在尝试访问数组的值("Id")。

这是我访问数组的代码:

$value[0]['Id']

这是错误:X

不能将字符串偏移量用作数组

我尝试访问的数组:Y

array(1) { [0]=> array(1) { ["Id"]=> string(2) "42"} }

舍入代码

$query = "select Id from test where Tags = " .  "'"$chosedOption[1]'""; 
    $result = mysql_query($query, $link);
    $value = mysqlArray($result);
    $value_id = null;
    $value_id = $value[0]['Id']; // gives X
    var_dump($value[0]['Id']); 
    var_dump($value); // gives Y

function mysqlArray($result) {
$table_result = array();
$r = 0;
while($row = mysql_fetch_assoc($result)) {
    $arr_row = array();
    $c = 0;
    while ($c < mysql_num_fields($result)) {
        $col = mysql_fetch_field($result, $c);
        $arr_row[$col -> name] = $row[$col -> name];
        $c++;
    }
    $table_result[$r] = $arr_row;
    $r++;
}
return $table_result; }

难道 $value_id 已经有一个(字符串)值吗?

将 mysqlArray($result); 替换为 mysql_fetch_array($result);