【问题标题】:Receiving data from an array within a function PHP从函数 PHP 中的数组接收数据
【发布时间】:2013-12-16 01:40:45
【问题描述】:

我对 PHP 还很陌生,并且遇到了数组问题。我刚刚设置了我的函数,其中包含一个数组。一切正常,除了当我在函数外部调用我的数组时,内部似乎什么都没有,(我的最后一个 for 循环没有打印任何内容)错误显示“注意:未定义的偏移量:每个数字”。

代码如下:

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

最后一个 for 循环是空的,但 getcoleachrow($col_num, $pos, $html); 会打印出我想要的每一个。

【问题讨论】:

  • 你可以 print_r($arr) 而不是循环遍历它, echo "
    " 也会很方便
  • 你可以从函数中返回$col或者让它通过引用传递。
  • 谢谢你,绝对没有数据存储在函数之外

标签: php arrays function global-variables scope


【解决方案1】:

你需要让你的函数返回一些东西,然后为返回的输出分配一个变量。在您的情况下,我正在考虑以下更改:

返回 $col;将此行添加到 getcoleachrow 函数的末尾,就在函数右花括号之前。

$col_num = getcoleachrow($col_num, $pos, $html);将其替换为以下行: getcoleachrow($col_num, $pos, $html);

【讨论】:

    【解决方案2】:

    不清楚是什么问题。函数getcoleachrow() 遍历数组和echos 值。那么最后的for($row = 0;$row&lt;=19;$row++){ 循环是做什么用的呢?事实上,再看看你的代码,不清楚$col_num 会做什么,因为你在第一行初始化它,将它发送到getcoleachrow() 函数,然后有那个奇怪的循环。缺了点什么。为清晰起见重新格式化您的代码并编辑原始帖子以提供帮助。

    编辑:根据原始发帖人的评论重构示例。该函数根本不返回值。让它返回一个值,你就可以开始了。

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

    【讨论】:

    • 很抱歉,我不明白这如何为 OP 添加内容,如果没有更改,您为什么不直接在重新格式化的代码中进行编辑?
    • @FranciscoPresencia 点。仍然不清楚问题是什么。重新格式化的代码添加到原始问题。
    • 我希望将函数添加到数组中,然后将该数据输入数据库,但函数外部似乎为空
    • 别人打败了我,但如果你的函数没有返回值,这是一个简单的例子。我添加了我对如何解决的看法。请也将其与其他人进行比较,以更好地了解如何处理函数和停用值。
    【解决方案3】:

    您永远不会返回值。您需要返回它并将其分配给一个变量,以便以后能够使用它。试试这个:

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

    不确定您的背景,但变量在默认情况下没有全局范围(通常在 PHP 中也不应该),就像在其他语言(如 javascript)中那样,数组也不是像在 C 中那样默认通过引用传递。但是,您实际上可以在 PHP 中返回一个数组,这非常方便 (;

    另一件事是你不需要声明变量来传递它(你不需要传递它),所以你的代码可能会更干净一些。

    【讨论】:

    • 从顶部删除$col_num = array();
    • 我刚看到这个!无论如何谢谢(;
    【解决方案4】:

    一些事情:

    1. 你的可选参数应该只出现在你的函数声明的末尾,所以你应该从第一个参数中删除= array()

      function getcoleachrow($col, $value, $html)
      
    2. 数组(就像标量一样)不是通过引用传递的,所以你可以:

      一个。在函数声明中指出:

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

      b.在函数末尾返回数组:

      function getcoleachrow($col, $value, $html)
      {
          // ...
          return $col;
      }
      $col_num = getcoleachrow($col_num, $pos, $html);
      
    3. 如果您总是使用空数组调用函数,则可能希望完全删除该参数:

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

    【讨论】:

      【解决方案5】:

      您的$col_num 数组未出现在范围内:

      你可以这样解决:

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

      但我不会用这个! 我会使用:

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

      然后

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

      使用函数的返回值

      【讨论】:

        【解决方案6】:

        您将所有值添加到数组中:$col 而不是数组 $col_num。 这就是为什么你不会在函数之外得到输出。

        $col = 数组();创建一个名为 $col 的新数组。

        改变

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

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

        或者干脆不用,在函数中使用$col_num

        【讨论】:

          【解决方案7】:

          您可以使用global 变量来解决您的问题。但我建议您返回新数组并将其分配给col_num。因为您修改了一个数组,但只是在函数内部,所以该数组的实例没有出现。
          尝试更改您的函数以返回这样的数组:

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

          并以这种模式调用你的函数:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-01-16
            • 1970-01-01
            • 2015-08-27
            • 2015-03-16
            • 1970-01-01
            • 1970-01-01
            • 2014-08-01
            相关资源
            最近更新 更多