duanhao1004 2014-12-02 16:39
浏览 19

来自函数的PHP数组

In trying to pass an array function from one php page to another. page try.php has the actual function

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

If i use

 echo "<pre>";
 print_r($types);
 echo "</pre>";

i get this

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
    )

)

But when I pass this function on try2.php and so the same pre, print_r to see what I get I get this

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
            )

    )

)

It's an array inside an array. Ive tried to echo out the array using foreach, and with a for loop, but I alway get undefined index or Array to string conversion and its just prints out "Array". What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • doutuo4285 2014-12-02 16:42
    关注

    Because you returned array($types);. Return $types instead of array($types).

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

    EDIT : In try.php try this code to show result in table

    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>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器