php - 函数动态返回值

时间:2009-07-06 10:22:50

标签: php function return

我想从函数返回一组值,直到它们存在...... 例如....

function abc($i="3"){

    for($a=1;$a<=$i;$a++) {
        $name='t'.$i;
        $$name = "ae".$a;
    }
    //now i am returning values 
    return array($t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10);
    //but i only want to return $t1,$t2,$t3 depending on $i
}

...谢谢

@therefromhere 我也在循环中创建一个数组,我将粘贴原始代码,以便您可以更好地理解它

function extracting_comments($table, $fields,$condition,$order,$limit){
        $query="SELECT ".$fields."
                FROM ".$table."
                WHERE ".$condition."
                ORDER BY ".$order."
                LIMIT ".$limit." ";
        if($stmt = $this->conn->prepare($query)) {
            $stmt->execute();
            $row = array_pad(array(), $stmt->field_count, '');
            $params = array();
                foreach($row as $k=>$v) {
                  $params[] = &$row[$k];
                  echo $params[0];
                }
            call_user_func_array(array($stmt,'bind_result'),$params);
            $i=0;
            while($stmt->fetch()) {
            $i++;
            $name='t'.$i;
            $$name = array();
            foreach ($row as $b=>$elem) {
            $atul[$b]=$row[$b];
            }
            $$name=$atul;
            }
            return array($t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10);
            $stmt->close();
        }

    }

现在他们只有5行数据所以他们没有回报$ t6,$ t7,$ t8,$ t9,$ t10 我想解决它,我正在使用

调用该函数
$extract=extracting_comments($table, $fields,$condition,$order,$limit);

请帮忙......谢谢

3 个答案:

答案 0 :(得分:10)

只需在for循环中构建数组:

function abc($i=3) {
    $array = array();
    for ($a=1; $a<=$i; $a++) {
        $array[] = "ae".$a;
    }
    return $array;
}

在您编辑问题后,向我们透露了您的实际问题,请参阅此处的建议:

function extracting_comments($table, $fields, $condition, $order, $limit) {
    $retVal = array();
    $query = "SELECT ".$fields."
        FROM ".$table."
        WHERE ".$condition."
        ORDER BY ".$order."
        LIMIT ".$limit." ";
    if ($stmt = $this->conn->prepare($query)) {
        $stmt->execute();
        $row = array_pad(array(), $stmt->field_count, '');
        call_user_func_array(array($stmt, 'bind_result'), $row);
        while ($stmt->fetch()) {
            $retVal[] = $row;
        }
        $stmt->close();
        return $retVal;
    }
}

答案 1 :(得分:1)

我相信这会对你有所帮助。你有非常复杂的代码,有很多副作用和bug,你最好考虑重新设置它。返回后放置语句也没有任何效果,因为它不会被调用。

function extracting_comments($table, $fields,$condition,$order,$limit){
                $query="SELECT ".$fields."
                                FROM ".$table."
                                WHERE ".$condition."
                                ORDER BY ".$order."
                                LIMIT ".$limit." ";
                if($stmt = $this->conn->prepare($query)) {
                        $stmt->execute();
                        $row = array_pad(array(), $stmt->field_count, '');
                        $params = array();
                                foreach($row as $k=>$v) {
                                  $params[] = &$row[$k];
                                  echo $params[0];
                                }
                        call_user_func_array(array($stmt,'bind_result'),$params);
                        $i=0;
                        $result = array();
                        while($stmt->fetch()) {
                        $i++;
                        foreach ($row as $b=>$elem) {
                        $atul[$b]=$row[$b];
                        }
                        $result[]=$atul;
                        }
                        $stmt->close();
                        return $result;
                }

        }

答案 2 :(得分:0)

随着你继续构建数组会更清晰,那么你就不需要临时变量了:

function abc($i="3") {
  $myArray = array();
  for($a=1;$a<=$i;$a++) {
    $myArray[] = "ae" . $a;  // add new values to the end of the array
  }

  return $myArray;
}

如果要检查变量是否存在(并且不为空),请使用isset()