Cannot Use String Offset as an Array in PHP

Cannot use string offset as an array in php

For PHP4

...this reproduced the error:

$foo    = 'bar';
$foo[0] = 'bar';

For PHP5

...this reproduced the error:

$foo = 'bar';

if (is_array($foo['bar']))
echo 'bar-array';
if (is_array($foo['bar']['foo']))
echo 'bar-foo-array';
if (is_array($foo['bar']['foo']['bar']))
echo 'bar-foo-bar-array';

(From bugs.php.net actually)

Edit,

so why doesn't the error appear in the
first if condition even though it is a
string.

Because PHP is a very forgiving programming language, I'd guess. I'll illustrate with code of what I think is going on:

$foo = 'bar';
// $foo is now equal to "bar"

$foo['bar'] = 'foo';
// $foo['bar'] doesn't exists - use first index instead (0)
// $foo['bar'] is equal to using $foo[0]
// $foo['bar'] points to a character so the string "foo" won't fit
// $foo['bar'] will instead be set to the first index
// of the string/array "foo", i.e 'f'

echo $foo['bar'];
// output will be "f"

echo $foo;
// output will be "far"

echo $foo['bar']['bar'];
// $foo['bar'][0] is equal calling to $foo['bar']['bar']
// $foo['bar'] points to a character
// characters can not be represented as an array,
// so we cannot reach anything at position 0 of a character
// --> fatal error

Fatal error: Cannot use string offset as an array?

you have used $result['info'] in a loop. it should be sometimes sub array of $result['info'] is not set.
It seems that you do not need to run loop. because you will have single user info at a time.
or you can use break like this:

foreach ($response as $result) {
$provider = strtolower($result['provider']);
$user = array(
'provider' => $result['provider'],
'id' => $result['uid'],
'name' => isset($result['info']['name']) ? $result['info']['name'] : '',
'image' => isset($result['info']['image']) ? $result['info']['image'] : '',
'link' => isset($result['info']['urls'][$provider]) ? $result['info']['urls'][$provider] : ''
);
print_r($user);
echo "<h1>" . $user['provider'] . "</h1>";
echo "<p>" . $user['id'] . "</p>";
echo '<p><img src="' . $user['image'] . '" /></p>';
echo "<p>" . $user['name'] . "</p>";
echo "<p>" . $user['link'] . "</p>";
break;
}

PHP 7 Upgrade Uncaught exception 'Error' with message 'Cannot use string offset as an array'

The issue here is that you can't successfully play with a STRING variable using ARRAY keys unless you are simply trying to return the Nth character of the string using $myString[n].

Repro:

$x = 'hello'
echo $x[1]; // returns 'e', i.e. the 1st char (0 based) of 'hello'
$x[4] = 'x';
echo $x; // returns 'hellx';

$x['my_key'] = 5; // Error illegal string offset

I.e. you can use array keys to access the character of a string (i.e. the string offset, but it will only allow you to use a valid offset within the length of the string. You can't use a random key on a variable already initialized as a string.

You need to make get_user_data return an array at all times. If it is empty, return [].

$user_data = get_user_meta( $user_id, 'all_contests', true ) ?: [];

How to solve cannot use string offset as an array error in PHP?

You have set $bno as string in some previous code.
What you can do for a quick fix is:

  1. change $bno to somehing else, for example $book
$book[0]['TitleNo'] = $_POST['bno'];  
$book[1]['TitleNo'] = $_POST['bno1'];
//..

  1. set $bno to a new array and then assign the values
$bno = array();
$bno[0]['TitleNo'] = $_POST['bno'];
$bno[1]['TitleNo'] = $_POST['bno1'];
//...

Additional Notes

By the way it's better to escape somehow the values you enter in your DB. You can use mysqli_real_escape_string

Just assign do this for all the values:



$bno[0]['TitleNo'] = mysqli_real_escape_string($db, $_POST['bno']);

Sources to read

http://php.net/manual/en/mysqli.real-escape-string.php

PHP message: PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string

First of all, welcome to Stack Overflow!

This error means that you are trying to access the index [1]['size'] of the string, which is not valid. Be sure to check that uploadSingleFile(...) is returning you an array and not a string.

I checked your code and I saw that the returned array of uploadSingleFile have these three items:

$uploadStatus = array($status, $newfilename, $errors);

$newfilename is not an array. It is a string, as you defined here:

$newfilename = $zoneCustomer . '-' . strtoupper($zone) . '-' . uniqid() . '.' . end($temp);

Cannot use string offset as an array Fatal Error

“Cannot use string offset as an array” Fatal Error

As shown in your code, $postvals is a string and not an array, so the key for attachment does not exist.

Changing:

$postvals = '';

To one of the following:

$postvals = array();
$postvals = [];

Will initialize the varaible as the correct type.

How to fix error: Cannot use string offset as an array

Many parts of your code are type-swapping your variables; mixing arrays and strings. Try the below:

for($i = $start; $i < $stop; $i++)
{
// Debug only. Remove once bugs are squashed.
if(!is_array($ret["content"])){
error_log("You've set ret['content'] as a non-array; probably a string!");
}
if(!is_array($ret["content"]["news"])){
error_log("You've set ret['content']['news'] as a non-array too!");
}
//end debug block
/***
* You do not need to count the array values each time, simply append with []
***/
$ret["content"]["news"][] = $this->getPost($all[$i]);
}


Related Topics



Leave a reply



Submit