Nic
Nic

Reputation: 2843

Receiving data from an array within a function PHP

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.

Upvotes: 0

Views: 112

Answers (7)

James Jones
James Jones

Reputation: 3899

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);

Upvotes: 0

Giacomo1968
Giacomo1968

Reputation: 26066

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];
}

Upvotes: 1

Francisco Presencia
Francisco Presencia

Reputation: 8851

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.

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173572

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);
    

Upvotes: 0

halfbit
halfbit

Reputation: 3939

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

Upvotes: 0

Patrick Aleman
Patrick Aleman

Reputation: 619

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

Upvotes: -1

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

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);

Upvotes: 1

Related Questions