Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 新手需要在课堂上更有效地处理数组_Php_Arrays_Class_Process - Fatal编程技术网

Php 新手需要在课堂上更有效地处理数组

Php 新手需要在课堂上更有效地处理数组,php,arrays,class,process,Php,Arrays,Class,Process,作为PHP类的新手,我试图找到从文本文件(电子邮件)中检索值的最佳方法。文本文件逐行转换为数组。我正在一次处理多个文本文件。从一些文本文件中,我需要检索比其他文件更多的信息,因此我创建了一个包含我需要的所有函数的类。简而言之,这就是我的想法: <?php $to_process = array( '9/24/15 11:03:04 PM Task Start', '[...]', '[...]', '9/24/15 11:25:54 PM Tas

作为PHP类的新手,我试图找到从文本文件(电子邮件)中检索值的最佳方法。文本文件逐行转换为数组。我正在一次处理多个文本文件。从一些文本文件中,我需要检索比其他文件更多的信息,因此我创建了一个包含我需要的所有函数的类。简而言之,这就是我的想法:

<?php
$to_process = array(
    '9/24/15 11:03:04 PM    Task Start',
    '[...]',
    '[...]',
    '9/24/15 11:25:54 PM    Task Stop',
    '    ',
    '    '
    );

    $message = new process;

    $start  = $message->retrieve_start(to_process);
    $stop   = $message->retrieve_stop(to_process);

class process {

    function retrieve_start($arr) {
        $start = explode(" ", $arr[0]);
        return $this->$start[1];
    }

    function retrieve_other($arr) {
        // do stuff
    }

    function retrieve_stop($arr) {
        // do other stuff
    }

}
?>


它完成了这项工作,但每次调用其中一个函数时,都会将数组传递给该函数。在我看来,这不是很有效。如何提高效率?

您可以使用构造函数方法将进程数组加载到对象中,就像这样

<?php
class process {

    protected $to_process = array();

    public function __construct($theProcessArray)
    {
        $this->to_process = $theProcessArray;
    }

    public function retrieve_start() 
    {
        $start = explode(" ", $this->to_process[0]);
        return $start[1];
    }

    public function retrieve_other() 
    {
        // do stuff
    }

    public function retrieve_stop() 
    {
        // do other stuff
    }
}

$to_process = array('9/24/15 11:03:04 PM    Task Start',
                    '[...]', '[...]',
                    '9/24/15 11:25:54 PM    Task Stop',
                    '    ', '    ');

$message = new process($to_process);

$start  = $message->retrieve_start($to_process);
$stop   = $message->retrieve_stop($to_process);
?>


处理不同的文本文件时,
$to\u处理
数组是否会更改?还是总是一样的?每个文本文件都是一个新数组。我正在将电子邮件处理到监控软件中。我更改:$start=$message->retrieve\u start(to\u process);to$start=$message->retrieve_start()让它工作……哦,是的,输入了一个错误,请原谅。