PHP - Warning: Cannot use a scalar value as an array in .....

Posted on 2015-01-28
Last Modified: 2015-02-02
Hello guys,

Well, this is the first time working with arrays, what I need is to check if my array $NOHABILES in column 2 has this value "00:00:00", so I can display what I need but I'm doing something wrong.

This is the error
Warning: Cannot use a scalar value as an array in C:\inetpub\wwwroot\Intranet2014\aplicaciones\citas\calendar0115.php on line 218

      foreach ($NOHABILES as $d => $x){
            switch ($x[1]) {
                     case 1:
                        if($NOHABILES[2] = '00:00:00') {
                              ?> <TD height="30" ALIGN=center bgcolor="#666666">1</a></TD> <?php
                              ?> <TD height="30" ALIGN=center bgcolor="#990000"><a href="horarios.php">1</a></TD> <?php
                  case 2:
                        if($NOHABILES[2] = '00:00:00') {
                              ?> <TD height="30" ALIGN=center bgcolor="#666666">2</a></TD> <?php
                              ?> <TD height="30" ALIGN=center bgcolor="#990000"><a href="horarios.php">2</a></TD> <?php
                  case 3:
                        if($NOHABILES[2] = '00:00:00') {
                              ?> <TD height="30" ALIGN=center bgcolor="#666666">3</a></TD> <?php
                              ?> <TD height="30" ALIGN=center bgcolor="#990000"><a href="horarios.php">3</a></TD> <?php
                        ?> </TR>
                        <TR> <?php
      ?> </TR>
Question by:elm9999
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40576254
I don't know what is causing your main problem but this is wrong.  It is assigning a value instead of making a comparison.  A comparison is done with '==' and assignment is '='.
 // current
if($NOHABILES[2] = '00:00:00')
// should be
 if($NOHABILES[2] == '00:00:00')

Open in new window


Expert Comment

by:Brian Tao
ID: 40576300
$x is an element of the array $NOHABILES, but in the switch statement, you're trying to use it as an array by $x[1].

Author Comment

ID: 40576372
Thank you Dave & Taoyipai :)

I already change to if($NOHABILES[2] == '00:00:00') but now the error is this:
Notice: Undefined offset: 2 in C:\inetpub\wwwroot\Intranet2014\aplicaciones\citas\calendar0115.php on line 116
my array contains this:
$result = mysqli_query($mysqli,"SELECT clave, anotramite,mestramite,diatramite,horatramite,COUNT(*) FROM transacciones WHERE anotramite = '$ano' && mestramite = '$mes' GROUP BY diatramite,horatramite ORDER BY anotramite, mestramite, diatramite, horatramite");
      if ($result) {
            while($row = mysqli_fetch_array($result)) {
                  $NOHABILES[$row[0]][1] = $row["diatramite"];
                  $NOHABILES[$row[0]][2] = $row["horatramite"];
                  $NOHABILES[$row[0]][3] = $row["COUNT(*)"];
                  $totalxdia = ($row["COUNT(*)"] += "$totalxdia");

So, how to make the switch statement?
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Accepted Solution

Brian Tao earned 500 total points
ID: 40576986
Should be something like this:
switch ($x) {
                     case 1:
                        if($NOHABILES[$d][2] == '00:00:00') {
// and the rest of your code

Open in new window


Author Closing Comment

ID: 40584886
Great help !!!

Thank you.

Expert Comment

by:Brian Tao
ID: 40585032
Thanks for the points.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question