0

I am trying to create a 2-dimensional array, reading specific values from a (large) met file. The arrays are empty and I dont know why. I have the following array:

$varmet = array('tasmax', 'tasmin', 'pr', 'clt');

I have two loops:

The first is

for ($j = 0; $j <= 3; $j++)  {
...

I read the corresponding files and variables, etc. It works properly.

Then I have the other loop

for ($i = 1; $i <= 360; $i++) {
....
$valor = $valor * $correctp;

It works perfectly, retrieving the required value in $valor

The problem arise when I want to store the value in a different array, according to the met variable:

switch ($j) {
case 0: 
   $tmax[$i] = $valor;
break;
case 1: 
   $tmin[$i] = $valor;
break;
case 2: 
   $prec[] = $valor;
break;
case 3: 
   $clt[$i] = $valor;
break; } // Fin del switch

    }  // Fin del for para un fichero met
} // Fin del for para todas las varmet

The $tmax, $tmin, etc. have no value, although $valor does have. Furthermore, if I assign the array within the loop (i.e. $valorest[$j][$i] = $valor) it still have the values, but not outside it.

Anybody knows what I am doing wrong?

1
  • 4
    Please show us your complete code, not only bits and pieces. Commented May 2, 2012 at 6:16

1 Answer 1

1

Try like this

$tmax = array(); //declare this outside the loop i.e before the loop starts

In switch case statement, replace the below line

$tmax[$i] = $valor;

with

array_push($tmax,$valor);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.