函数中的PHP数组
在尝试将数组函数从一个php页面传递到另一个php页面时。page try.php具有实际功能函数中的PHP数组,php,arrays,Php,Arrays,在尝试将数组函数从一个php页面传递到另一个php页面时。page try.php具有实际功能 function someFunc() { include_once("includes/dbconnect.php"); $types = array(); $connect = TRUE; $dbconn = dbconnect($connect); if ($dbconn) { $sql = "select usertype, userdeptnum from bd.bdusers"
function someFunc() {
include_once("includes/dbconnect.php");
$types = array();
$connect = TRUE;
$dbconn = dbconnect($connect);
if ($dbconn) {
$sql = "select usertype, userdeptnum from bd.bdusers";
$stmt = db2_prepare($dbconn, $sql);
if ($stmt) {
$result = db2_execute($stmt);
if (!$result) {
echo "exec errormsg: " . db2_stmt_errormsg($stmt);
}
while ($row = db2_fetch_assoc($stmt)) {
$types[] = $row;
}
} else {
echo "exec errormsg: " . db2_stmt_errormsg($stmt);
}
db2_close($dbconn);
} else {
echo "faild " . db2_conn_errormsg();
}
return array($types);
}
如果我使用
echo "<pre>";
print_r($types);
echo "</pre>";
但是当我在try2.php上传递这个函数时,同样的pre,print\r来看看我得到了什么,我得到了这个
function someFunc() {
include_once("includes/dbconnect.php");
$types = array();
$connect = TRUE;
$dbconn = dbconnect($connect);
if ($dbconn) {
$sql = "select usertype, userdeptnum from bd.bdusers";
$stmt = db2_prepare($dbconn, $sql);
if ($stmt) {
$result = db2_execute($stmt);
if (!$result) {
echo "exec errormsg: " . db2_stmt_errormsg($stmt);
}
while ($row = db2_fetch_assoc($stmt)) {
$types[] = $row;
}
} else {
echo "exec errormsg: " . db2_stmt_errormsg($stmt);
}
db2_close($dbconn);
} else {
echo "faild " . db2_conn_errormsg();
}
return $types; //<==
}
它是数组中的一个数组。我尝试使用foreach和for循环回显数组,但我总是得到未定义的索引或数组到字符串的转换,它只是打印出“array”。
我做错了什么?因为您返回了
数组($types)代码>。返回$types
而不是数组($types)
因为您返回了数组($types)代码>。返回$types
而不是数组($types)
因为您返回了数组($types)代码>。返回$types
而不是数组($types)
因为您返回了数组($types)代码>。返回$types
而不是数组($types)
我更改了这一行并刷新了try2.php,使用pre我得到了外观正确的数组,但是foreach给了我这个错误“注意:第17行数组上的/www/webvr6/try2.php中的数组到字符串转换”我试图用这个数组填充一个表。有更好的方法吗?我更改了这一行并刷新了try2.php,使用pre我得到了外观正确的数组,但是foreach给了我这个错误“注意:第17行数组上的/www/webvr6/try2.php中的数组到字符串的转换”我试图用这个数组填充一个表。有更好的方法吗?我更改了这一行并刷新了try2.php,使用pre我得到了外观正确的数组,但是foreach给了我这个错误“注意:第17行数组上的/www/webvr6/try2.php中的数组到字符串的转换”我试图用这个数组填充一个表。有更好的方法吗?我更改了这一行并刷新了try2.php,使用pre我得到了外观正确的数组,但是foreach给了我这个错误“注意:第17行数组上的/www/webvr6/try2.php中的数组到字符串的转换”我试图用这个数组填充一个表。有更好的方法吗?
Array
(
[0] => Array
(
[0] => Array
(
[USERTYPE] => 1
[USERDEPTNUM] => 3
)
[1] => Array
(
[USERTYPE] => 1
[USERDEPTNUM] => 5
)
[2] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 28
)
[3] => Array
(
[USERTYPE] => 1
[USERDEPTNUM] => 28
)
[4] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 3
)
[5] => Array
(
[USERTYPE] => 1
[USERDEPTNUM] => 1
)
[6] => Array
(
[USERTYPE] => 1
[USERDEPTNUM] => 1
)
[7] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 3
)
[8] => Array
(
[USERTYPE] => 1
[USERDEPTNUM] => 31
)
[9] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 2
)
[10] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 1
)
[11] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 56
)
[12] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 89
)
[13] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 56
)
[14] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 45
)
[15] => Array
(
[USERTYPE] => 2
[USERDEPTNUM] => 90
)
)
)
function someFunc() {
include_once("includes/dbconnect.php");
$types = array();
$connect = TRUE;
$dbconn = dbconnect($connect);
if ($dbconn) {
$sql = "select usertype, userdeptnum from bd.bdusers";
$stmt = db2_prepare($dbconn, $sql);
if ($stmt) {
$result = db2_execute($stmt);
if (!$result) {
echo "exec errormsg: " . db2_stmt_errormsg($stmt);
}
while ($row = db2_fetch_assoc($stmt)) {
$types[] = $row;
}
} else {
echo "exec errormsg: " . db2_stmt_errormsg($stmt);
}
db2_close($dbconn);
} else {
echo "faild " . db2_conn_errormsg();
}
return $types; //<==
}
echo '<table border="1">';
echo '<tr><th>USERTYPE</th><th>USERDEPTNUM</th></tr>';
foreach($types as $val){
echo '<tr><td>'.$val['USERTYPE'].'</td><td>'.$val['USERDEPTNUM'].'</td></tr>';
}
echo '</table>';
<?php
include_once("try.php");
$types = someFunc();
echo "<pre>";
print_r(someFunc());
echo "</pre>";
foreach ($types as $list){
echo $types;
}
?>