0

I'm fairly new to PHP and having a problem with an array. I have just set up my function with an array in it. Everything is working fine except when I call my array outside the function it seems nothing is inside, (my last for loop prints nothing out) the error displays "Notice: Undefined offset: for each number".

Here is the code:

$col_num = array();

// `getcoleachrow()`: function to get each data in each column row
function getcoleachrow ($col = array(), $value, $html){

  // 220 cells in the table
  for ($value=$value;$value<220;$value+=11){

    //gets the data from a cell then turn it to text and stores it in an array selected
    array_push($col, $html->find('td', $value)->plaintext);
  }

  // 20 rows and 20 data from the table in an array
  for ($rows = 0;$rows<=19;$rows++) {

    //print out array
    echo $col[$rows];
  }

} // getcoleachrow

// Call the `getcoleachrow()` function
getcoleachrow($col_num, $pos, $html);
//getcoleachrow($col_team, $team);


// Goes through every row
for($row = 0;$row<=19;$row++){
  echo $col_num[$row];
}

The last for loop is empty but the getcoleachrow($col_num, $pos, $html); prints out every I want.

3
  • you can print_r($arr) instead of loop through it, also echo "<pre>" will be handy
    – Ben
    Commented Nov 29, 2013 at 20:26
  • You can return $col from the function or have it pass by reference.
    – Ja͢ck
    Commented Nov 29, 2013 at 20:26
  • Thanks for that, definitely no data is stored outside the function
    – Nic
    Commented Nov 29, 2013 at 20:28

7 Answers 7

1

You can solve your problem using global variable. But I advise you to return the new array and assign it to col_num. Because you modify an array but is only inside the function the instance of that array not out.
try to change your function to return an array like this:

function getcoleachrow($col = array(), $value, $html){

   //220 cells in the table
   for($value=$value;$value<220;$value+=11){

       //gets the data from a cell then turn it to text and stores it in an array selected
      array_push($col, $html->find('td', $value)->plaintext);
   }

   //20 rows and 20 data from the table in an array
   for($rows = 0;$rows<=19;$rows++){

       //print out array
       echo $col[$rows];
   }
   return($col);
}

and call your function in this mode:

$col_num = getcoleachrow($col_num, $pos, $html);
1

Unclear what the issue is. The function getcoleachrow() iterates through an array & echos the values. Then what is the final for($row = 0;$row<=19;$row++){ loop for? In fact looking at your code some more it is unclear what $col_num will do since you init it in the first line, send it to the getcoleachrow() function and then have that odd loop after that. Something is missing. Reformatted your code for clarity & edited original post to help.

EDIT: Refactoring the example based on the original poster’s comment. The function is simply not returning a value. Have it return a value & you are good to go.

// `getcoleachrow()`: function to get each data in each column row
function getcoleachrow ($col = array(), $value, $html){

  // Initing the array.
  $col_num = array();

  // 220 cells in the table
  for ($value=$value;$value<220;$value+=11){

    //gets the data from a cell then turn it to text and stores it in an array selected
    array_push($col, $html->find('td', $value)->plaintext);
  }

  // 20 rows and 20 data from the table in an array
  for ($rows = 0;$rows<=19;$rows++) {

    //print out array
    $col_num[] = $col[$rows];
  }

  // Return the `$col_num` array.
  return $col_num;

} // getcoleachrow

// Call the `getcoleachrow()` function
$col_num = getcoleachrow($col_num, $pos, $html);
//$col_num = getcoleachrow($col_team, $team);


// Goes through every row
for($row = 0;$row<=19;$row++){
  echo $col_num[$row];
}
2
  • I'm sorry but I don't see how this adds something to the OP, why didn't you just edit in the reformatted code if it's unchanged? Commented Nov 29, 2013 at 20:34
  • I wanted the function to add to the array and then input that data into a database but it seems empty outside the function
    – Nic
    Commented Nov 29, 2013 at 20:37
0

Your $col_num array does not apear in scope:

you could solve this like:

function getcoleachrow(&$col = array(), $value, $html){
-----------------------^ this here
.... 
}

$col_num=array()
getcoleachrow($col_num, $pos, $html);

but i would not use this! I would use:

function getcoleachrow($col = array(), $value, $html){
...
return $col // these is my interest
}

and then

$col_num= getcoleachrow($col_num, $pos, $html);

use the returnvalue of the function

0
0

A few things:

  1. Your optional arguments should only appear at the end of your function declaration, so you should remove the = array() from the first argument:

    function getcoleachrow($col, $value, $html)
    
  2. Arrays (just like scalars) aren't passed by reference, so you could:

    a. indicate that in the function declaration:

    function getcoleachrow(&$col, $value, $html)
    

    b. return the array at the end of your function:

    function getcoleachrow($col, $value, $html)
    {
        // ...
        return $col;
    }
    $col_num = getcoleachrow($col_num, $pos, $html);
    
  3. If you're always calling the function with an empty array, you may wish to drop the argument altogether:

    function getcoleachrow($value, $html)
    {
        $col = [];
        // ...
        return $col;
    }
    $col_num = getcoleachrow($pos, $html);
    
0
0

You are never returning the values. You need to return it and assign it to a variable to be able to use it afterwards. Try this instead:

//function to get each data in each column row
function getcoleachrow($value, $html) {
  $col = array();

  //220 cells in the table
  for($value=$value;$value<220;$value+=11){

    //gets the data from a cell then turn it to text and stores it in an array selected
    array_push($col, $html->find('td', $value)->plaintext);
    }

  // Return it
  return $col;
  }

// No need to pass the array if you follow this path (returning the array)
$col_nums = getcoleachrow($pos, $html);

//Goes through every row
for($row = 0;$row<=19;$row++){
  echo $col_num[$row];
  }

Not sure on your background, but variables do not have global scope by default (nor should normally in PHP) as they do in other languages like javascript, nor the arrays are passed by reference as default like they are in C. However, you can actually return an array in PHP, something that comes very handy (;

Another thing is that you don't need to declare the variable to pass it (you don't need to pass it), so your code could be a bit cleaner.

2
  • Remove $col_num = array(); from top.
    – The Alpha
    Commented Nov 29, 2013 at 20:30
  • I just did before seeing this! Thanks anyway (; Commented Nov 29, 2013 at 20:31
0

You need to have your function return something and then assign a variable to that returned output. In your case I'm thinking the following changes:

return $col; add this line at the end of the getcoleachrow function just before the function closing curly bracket.

$col_num = getcoleachrow($col_num, $pos, $html); subsitute this for the line: getcoleachrow($col_num, $pos, $html);

-1

You added all the values to the array: $col and not the array $col_num. Thats why you dont get output outside the function.

$col = array(); makes an new array named $col.

change

function getcoleachrow($col = array(), $value, $html){

to

function getcoleachrow($col, $value, $html){

Or just leave it out and use $col_num in the function

Not the answer you're looking for? Browse other questions tagged or ask your own question.