【问题标题】:Trying to pass an array outside the scope into a function for echo'ing试图将范围外的数组传递给函数以进行回显
【发布时间】:2010-10-14 16:12:40
【问题描述】:

各位程序员,大家好,

我有一个问题(除了不够了解)我需要知道如何在一个函数中将一个数组从作用域外部传递到内部,然后在一个函数中回显一个特定的数组索引。

我一直在网上寻找解决方案,向其他程序员寻求帮助,但到目前为止没有任何效果。

在包含文件中我正在创建数组:

$errmsg[0] = 'the message is too short, please enter more than 10 charaters.';
$errmsg[1] = 'the message is too long, please enter less than 1000 charaters.';

然后我继续序列化数组以保存它。

$e = serialize($errmsg);

然后在另一个包含文件中创建我的函数。

function contact($e) {
    echo unserialize($errmsg[0]);
}

最后在主 index.php 文件中我回调函数。

contact($e);

现在这当然行不通,如果有任何好心人能让我走上正轨,甚至给我解决这个问题的解决方案,我将不胜感激。

如果您需要我提供任何进一步的信息,请说。

附言我现在已经完成了当天的工作,所以我的回复要到格林威治标准时间今晚晚些时候。

【问题讨论】:

    标签: php arrays function serialization


    【解决方案1】:
    function contact() {
        global $e;
        $unserialize =  unserialize($e);
        echo $unserialize[0];
    }
    
    contact();
    

    更好的是:

    $errmsg[0] = '...'; $errmsg[1] = '...';
    
    $e = serialize($errmsg);
    
    function concact($e) {
        $array = unserialize($e);
        echo $array[0];
    }
    
    // now maybe: contact($e);
    

    【讨论】:

    • 发挥了作用,:) 简直不敢相信我所缺少的只是将序列化数组作为普通数组回调,然后将其回显。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2016-03-02
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多