Hi all I'm new to oop and I wanted to find out how to return multiple array variable from a function. Please see below for an explanation
大家好我是oop的新手,我想知道如何从函数中返回多个数组变量。请参阅下面的解释
function getvalues(){
//do mysql query using pdo
while($row = $getmostvalues->fetch(PDO::FETCH_ASSOC)) {
$value[] = $row['val1'];
$time[] = $row['time'];
}
}
how can I retrieve and use say $value[0] in my php code after calling getvalues();
如何在调用getvalues()之后在我的php代码中检索并使用say $ value [0];
1 个解决方案
#1
0
The problem you're looking for an answer on doesn't have anything to do with OOP, but here's your answer. It's more of a question on how to use arrays.
你正在寻找答案的问题与OOP没有任何关系,但这是你的答案。这是关于如何使用数组的更多问题。
function getvalues(){
while($row = $getmostvalues->fetch(PDO::FETCH_ASSOC)) {
$value[] = $row['val1'];
$time[] = $row['time'];
}
return array($value,$time);
}
$retval = getvalues();
$arrValues = $retval[0];
$arrTimes = $retval[1];
Value at index of the values array would then be $arrValues[0]
.
值数组的索引处的值将是$ arrValues [0]。
If you're trying to return an object:
如果您正在尝试返回一个对象:
return (object) array('value' => $value, 'time' => $time);
return(object)array('value'=> $ value,'time'=> $ time);
This would return an object with two arrays accessible via $retval->value
and $retval->time
这将返回一个具有两个数组的对象,可通过$ retval-> value和$ retval-> time访问
If you don't want arrays, cast those arrays as objects as well.
如果您不想要数组,也可以将这些数组转换为对象。
#1
0
The problem you're looking for an answer on doesn't have anything to do with OOP, but here's your answer. It's more of a question on how to use arrays.
你正在寻找答案的问题与OOP没有任何关系,但这是你的答案。这是关于如何使用数组的更多问题。
function getvalues(){
while($row = $getmostvalues->fetch(PDO::FETCH_ASSOC)) {
$value[] = $row['val1'];
$time[] = $row['time'];
}
return array($value,$time);
}
$retval = getvalues();
$arrValues = $retval[0];
$arrTimes = $retval[1];
Value at index of the values array would then be $arrValues[0]
.
值数组的索引处的值将是$ arrValues [0]。
If you're trying to return an object:
如果您正在尝试返回一个对象:
return (object) array('value' => $value, 'time' => $time);
return(object)array('value'=> $ value,'time'=> $ time);
This would return an object with two arrays accessible via $retval->value
and $retval->time
这将返回一个具有两个数组的对象,可通过$ retval-> value和$ retval-> time访问
If you don't want arrays, cast those arrays as objects as well.
如果您不想要数组,也可以将这些数组转换为对象。