【问题标题】:php array not received correctly in class constructor类构造函数中未正确接收php数组
【发布时间】:2015-03-16 01:53:28
【问题描述】:

我正在尝试将数组传递给类构造函数,但由于某种原因,构造函数接收的数组与我发送的数组不同。 构建 2 个数组并发送它们的代码:

$exportArray=array();
            foreach($new->arr as $ar){
                $values[]=intval($ar[4]);
                $dates[]=strtotime($ar[1]);
                $exportArray[]=array($ar[5],$ar[0],$ar[2],$ar[3],$ar[1]);
            }
            $new->query="SELECT distinct c_name,date,m_name,t_amount,t_id FROM transport,customer,driver,material WHERE"
                    . " customer.c_id=transport.c_id AND material.m_id=transport.m_id AND transport.d_id=$name ORDER BY date DESC LIMIT $lim";
            //var_dump($new->query);
            $new->again();
            foreach($new->arr as $ar){
                echo "<tr><td><a href='customers.php?c_name=$ar[0]'>$ar[0]</a></td><td>$ar[1]</td><td><a href='materials.php?m_name=$ar[2]'>$ar[2]</a></td><td>$ar[3]</tr>";
                //var_dump($ar);
            }
            ?>
        </table>
  <?php
    if(isset($lim)&&$lim!="1"){
        $qr=new query("SELECT last FROM graph");
        $last=$qr->arr[0][0];
            var_dump($values);
            var_dump($dates);
        $graph1=new graphs($dates,$values,"Deliveries With Respect To Dates","driver");
        $graph1->getGraph("date");
        $qr->again();

类构造函数接收它们并执行检查:

class graphs{

    public $xtype=NULL;
    public $ytype=NULL;
    public $graph=NULL;
    private $xs=array();
    private $ys=array();
    public $type=NULL;

    public function __construct($arr1,$arr2,$title,$type){
        if(!is_array($arr1)||!is_array($arr2)){
            die("in order to see the graph, you need more than one result!");
        }
        elseif(count($arr1)<=1||count($arr2)<=1){
            var_dump($arr1);
            var_dump($arr2);
            die("in order to see the RELEVANT graph, you need more than one result!");
        }
        $this->title=$title;
        $this->xs=$arr1;
        $this->ys=$arr2;
        $this->graph= new Graph(600,400,'auto');
        $this->graph->SetScale("textlin");
        $this->graph->SetShadow();
        $this->graph->title->Set($this->title);
        $this->graph->title->SetFont(FF_ARIAL,FS_NORMAL,9);
        $this->graph->xaxis->setTickLabels($arr1);
        if($type=="driver"){
            $this->type=1;
        }
        elseif($type=="customer"){
            $this->type=2;
        }
        elseif($type=="material"){
            $this->type=3;
        }
    }//construct

在浏览器上获取这个(前 2 个转储是我发送的,第二个是班级正在转储的):

【问题讨论】:

  • 请向我们展示包含类定义和所有内容的完整脚本
  • 我无法复制 - 你确定代码会给你那个错误吗?因为根据错误,您以某种方式获得了字符串customers,它根本不存在于您的输入数据中。 phpfiddle.org/main/code/szvx-6byt
  • 添加了更多代码信息
  • 如果你在构造函数的顶部使用print_r(),输出是什么? (同时注释掉所有其他代码行)
  • print_r() 在构造函数声明之后提供正确的信息。

标签: php arrays class constructor


【解决方案1】:

我认为您在构造函数中进行了一些初始化,尤其是对于您的 $arr1$arr2 参数

   $this->.. = $arr1;
   $this->.. = $arr2 ;

应该在你的if(!is_array($arr1)||!is_array($arr2)){..之前声明

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    相关资源
    最近更新 更多