PHP: Check 0?

I use a class that returns me the value of a specific row and cell of an Excel table. To create an array from a single column, I count the rows, and then iterate over this number with a loop for(), and then use $array[] = $valueto set the value of an increasing array object.

This works great if none of the values ​​in the cell are 0. The class returns me the number 0, so it has nothing to do with the class, I think this is how I iterate over the rows and then assign them to the array ... I want to transfer the value 0, because I create graphs with the data afterwards, here is the code i have.

// Get Rainfall
$rainfall = array();
for($i=1;$i<=$count;$i++)
{
    if($data->val($i,2) != 'Rainfall') // Check if not the column title
    {
        $rainfall[] = $data->val($i,2);
    }
}

For your information, it $datais an Excel spreadsheet object, and a method $data->val(row,col)is what returns the value to me. In this case, I get the data from the column 2.

Spreadsheet screenshot

+3
source share
4 answers

Have you tried array_push ()?

array_push($rainfall, $data->val($i,2));
+4
source

I would use strict comparison with non-identical here, instead of using the non equals operator:

if($data->val($i,2) !== 'Rainfall')

$data->val($i,2) , ==, , , , , . == === "RainFall" :

0 == "RainFall" : true
0 != "RainFall" : false
0 === "RainFall" : false
0 !== "RainFall" : true
+3

, 0 false, , . - ( )?

(int)($data->val($i,2));

(float)($data->val($i,2);)
+1

if. , , PHP, .

, . switch. , === ! ==, , .

You can read more here http://php.net/manual/en/language.operators.comparison.php .

Update: the if statement will not work in the case 0, because it (int)"Rainfall"will be specified in 0using PHP, forcing the operator to be if (0 != 0) { ... }.

If $irepresents a line number, why don't you start with 2 instead of 1?

0
source

Source: https://habr.com/ru/post/1747541/


All Articles