Need Help with Array and HTML

Hi All,

Although I am not a beginner you could say I am still a little green when it comes to PHP coding. With that said, I am having a very strange problem when I try to use a multidimensional array in my PHP code below.

for ( $x = 0; $x < count($uName); $x++ ) {
    echo "<br>".$uName[$x][0].", ".$uName[$x][1].", ".$uName[$x][2]."\n";
}

Array is created using “$uName = array(array(array()));”
This code works just fine when I execute it from the command line (see below),

Kitchen, sonoff_134, 1
Bedroom Wall-outlet, sonoff_194, 1
C3PO, sonoff_163,
Garage Outlet, sonoff_141, 1
Garage Door, sonoff_182,
Garland Lights, sonoff_186, 0
Christmas Tree, sonoff_111, 1

but when it runs in the embedded HTML code I get the following on my web page.

Kitchen, sonoff_134,
Bedroom Wall-outlet, sonoff_194,
C3PO, sonoff_163,
Garage Outlet, sonoff_141,
Garage Door, sonoff_182,
Garland Lights, sonoff_186,
Christmas Tree, sonoff_111,

I have been working on this for some time now and have tried everything I can think of. If there are any ideas out there please enlighten me. If more code is needed just let me know.

Thank you

Are the 1/0 values boolean (true/false)? Boolean false values don’t display when echoed on a web page, but I think they do show as zeros when the output is sent to the command line. Boolean true values do display as a 1 when echoed on a web page.

It would take seeing what the raw data looks like and what the ‘view source’ of the output in your browser shows. Add the following immediately before the loop producing the output -

echo '<pre>'; var_dump($uName); echo '</pre>';

Then, what does the ‘view source’ of the resultant web page look like?

That you for your extremely fats response. I hope I don’t miss any of your questions.

  1. Values
  2. The raw data is generates from a txt file the first two elements of the array. The third element comes from the output if an MQ sub call that I call using the shell_exec command.
  3. The output from the command you posted is the following:
    array(7) {
    [0]=>
    array(3) {
    [0]=>
    string(7) “Kitchen”
    [1]=>
    string(10) “sonoff_134”
    [2]=>
    string(0) “”
    }
    [1]=>
    array(3) {
    [0]=>
    string(19) “Bedroom Wall-outlet”
    [1]=>
    string(10) “sonoff_194”
    [2]=>
    string(0) “”
    }
    [2]=>
    array(3) {
    [0]=>
    string(4) “C3PO”
    [1]=>
    string(10) “sonoff_163”
    [2]=>
    string(0) “”
    }
    [3]=>
    array(3) {
    [0]=>
    string(13) “Garage Outlet”
    [1]=>
    string(10) “sonoff_141”
    [2]=>
    string(0) “”
    }
    [4]=>
    array(3) {
    [0]=>
    string(11) “Garage Door”
    [1]=>
    string(10) “sonoff_182”
    [2]=>
    string(0) “”
    }
    [5]=>
    array(3) {
    [0]=>
    string(14) “Garland Lights”
    [1]=>
    string(10) “sonoff_186”
    [2]=>
    string(0) “”
    }
    [6]=>
    array(3) {
    [0]=>
    string(14) “Christmas Tree”
    [1]=>
    string(10) “sonoff_111”
    [2]=>
    string(0) “”
    }
    }

I have no problem showing the code, but it is 152 lines long and I’m not sure what the limits are on here.

Thank you again

The environment that php runs in is different between the command line and a web server.

Do you have php’s error_reporting set to E_ALL and display_errors set to ON, so that php will report and display all the errors it detects?

Posting that amount of code shouldn’t be a problem.

You are the best. By looking at the output of the code you sent me I was able to see the array from the MQ messages was not getting populated. When I looked at that part of the code, I saw I had the incorrect path to my bash script I am running for those messages. Once I fixed that the output now looks correct. If you would mind taking a look at my code I will post it. I’m sure there are more efficient ways of doing a lot of what is in there.

Capture

Sponsor our Newsletter | Privacy Policy | Terms of Service