Forum Moderators: coopster

Message Too Old, No Replies

Scalar values as arrays

A bizarre error message that only occurs during a for loop

         

sawatkins

9:59 pm on May 19, 2008 (gmt 0)

10+ Year Member



The error message I'm getting is:

Warning: Cannot use a scalar value as an array in a_calendar_scheduledhours.php on line 186

Here's what I'm doing

while ($result_row = mysql_fetch_row($result)) {
$shifttypesplit = explode(" ", $result_row[0]);
$hours[shifttypesplit[1]] ++; //add them to an array, with the name as the key, and the number of shifts they're on as the value
}

The line that PHP doesn't like is $hours[shifttypesplit[1]] ++;

The problem is, I'm using the EXACT same code on another page. On this page, It's nested inside a FOREACH loop, but otherwise I'm literally doing THE EXACT same thing.

Does anyone have any ideas what's going on?

Little_G

10:02 pm on May 19, 2008 (gmt 0)

10+ Year Member



Hi,

$hours[shifttypesplit[1]]
should be:
$hours[$shifttypesplit[1]]

Andrew

g1smd

10:08 pm on May 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's the sort of error that I stare at for an hour or two before I spot it.

sawatkins

6:31 pm on May 20, 2008 (gmt 0)

10+ Year Member



Nope, sorry, that's not it.

I actually made that error when relaying it to you ... I'm still getting that error.

Can anyone help?

Little_G

7:56 pm on May 21, 2008 (gmt 0)

10+ Year Member



Hi,

This error is caused because you are trying to access a variable as an array (using $var[index]) but it isn't an array.
Of the four lines you posted which is line 186?

Andrew

sawatkins

8:35 pm on May 22, 2008 (gmt 0)

10+ Year Member



Line 186 is:

$hours[shifttypesplit[1]] ++

So, what you're saying is that $hours is not an array?

Little_G

9:57 pm on May 23, 2008 (gmt 0)

10+ Year Member



Hi,

Yes, that looks like the most likely cause of your problem.

Andrew

sawatkins

6:30 pm on May 27, 2008 (gmt 0)

10+ Year Member



Well it's about to get crazier Andrew. You see, $hours[] is in fact, an array. (I know, because I used is_array() to find out).

The array itself is causing the problems. I failed to notice earlier that I don't get this error message the FIRST time around the foreach() loop. It gives the error every other time after that.

So, I did what any sensible person would do and tried to unset() and empty() the array. But it didn't work. I *also* tried changing the array so that instead of $hours[$shifttypesplit[1]], it's now $hours[$key][$shifttypesplit[1]], but that didn't seem to help either.

It appears you don't really understand what's happening either. Does anyone truly know what's going on here?

gabidi

3:17 am on May 28, 2008 (gmt 0)

10+ Year Member



$hours['$shifttypesplit[1]'] or $hours['shifttypesplit[1]']

Emphasis being the single quotes around your inner array element.

jatar_k

3:50 am on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



or you could

$where = shifttypesplit[1];
echo $hours[$where];

Little_G

10:19 pm on May 28, 2008 (gmt 0)

10+ Year Member



Hi,

If

$hours
isn't causing the problem then it must be
$shifttypesplit
, if the
explode
function is passed an empty string as a delimiter it will return false, double check that there is in fact a space in between the two double-quotes.

Failing that, when things aren't doing what I expect them to do putting

var_dump
's [php.net] everywhere normally reveals the problem eventually.

Also make sure that your error reporting is printing Notices as well as Warnings, things like non-existent variables are Notices.

Andrew