Cannot use string offset as an array

Hi Guys

My first post and I want help…I hate people like that! :wink:

I’m trawling though the error logs of my website when I found an error message:
373] mod_fcgid: stderr: PHP Fatal error: Cannot use string offset as an array in /httpdocs/includes/getbasketdata.php on line 10.

I’ve’ve found line (10) in getbasketdata.php:

[php]
if (strtolower($parray[‘stocklevel’]) == “out”) {
if ($parray[‘restockdt’] != “”) {
$addline = “<span class=“addbutton”>ETA " . $parray[‘restockdt’] . “”;
} else {
$addline = “Out of stock”;
}
} else {
$optlist = “”;
if ($parray[‘size’][0] != “”) {
foreach ($parray[‘size’] as $c) {
$optlist .= “” . $c . “”;
}
$basketline .= “<span class=“popts”><a href=“sizes.html”>Size/Opt " . “<select size=1 name=”” . strtoupper($prodcode) . “m”>” . $optlist . “”;
}
[/php]

  • (error is on line 63 or a larger document. The above is an extract. Let me know if you want the whole thing)

From what I understand the array contains a number of objects ([‘pcode’], [‘condition’], [‘price’] etc) one of them being [‘size’] and clearly [‘size’] can, under some circumstances not be compared against “”. For what it’s worth, [‘size’] can contain an alpha numeric with a dash (-) ie ‘34L-36L’ or it can be empty.

This is an occasional error and the code is ancient! My knowledge of PHP is poor and the code looks fine to my eyes. I’ve tried to understand the
[php]$foo=‘bar’;
$foo[0]=‘bar’;
echo $foo[‘bar’][‘bar’];[/php]
examples google brought up as an answer to the error message but I just don’t get the relevance.

Help please! :o

Lenny

Ok, more googling and I think I may have found something. Apparently one reason the error message occurs is if the array object has not been set as opposed to being empty. I’ve inserted and ‘isset’ statement to check the object has been set and I get the following code:

[php]
if (isset($parray[‘size’][0])) && ($parray[‘size’][0] != “”) {
foreach ($parray[‘size’] as $c) {
$optlist .= “” . $c . “”;
}
[/php]

What do you think?

Have you tested and does it work? I find the best way to figure out something is to try it.

Sponsor our Newsletter | Privacy Policy | Terms of Service