开发者

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

开发者 https://www.devze.com 2023-03-04 17:04 出处:网络
I\'m trying to access the value (\'Id\') of an array. This is my code to access the array: $value[0][\'Id\']

I'm trying to access the value ('Id') of an array.

This is my code to access the array:

$value[0]['Id']

This is the error: X

Cannot use string offset as an array

The array that I try to access: Y

array(1) { [0]=> array(1) { ["Id"]=>开发者_运维问答 string(2) "42"} }

The surounding code

$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; }


Can it be that $value_id already has a (string) value?


Replace mysqlArray($result); with mysql_fetch_array($result);

0

精彩评论

暂无评论...
验证码 换一张
取 消