将值附加到类中的数组?

时间:2021-01-03 17:35:12

标签: php oop

####更新: 我有一个 curl 连接方法,它有一个数组来设置选项,我需要在 listDirectory 方法中附加一个选项并将其发送到第一个方法(连接)

我想将方法​​二中的值附加到方法一中的数组中 当我调用方法一时,我想显示:

Array ( [one] => 111 [two] => 2222[three] => 3333) 

有人可以帮忙吗?


<?php

class test{
   
   public $myarray;
   
   public function one(){
      
     return $this->myarray=array(
      "one"=>"111",
      "two"=>"222"
      );
      
   }
   
   public function two(){
      
      return $this->myarray["three"]="333";
   }
}
$myclass=new test();
$myclass->one();
$myclass->two();


print_r($myclass->one());

1 个答案:

答案 0 :(得分:-1)

我不知道你到底需要什么。我认为是这样的:

class test{
   
   public $myarray = [];
   
   public function add(array $array = []){
     return $this->myarray = array_merge($this->myarray, $array);    
   }

   public function get(){
     return $this->myarray;
   }
}

$myclass=new test();

$arr0 = $myclass->get();
// array(0) { }

$arr1 = $myclass->add(["one"=>"111","two"=>"222"]);  
//array(2) { ["one"]=> string(3) "111" ["two"]=> string(3) "222" }

$arr2 = $myclass->add(["three" =>"333"]); 
//array(3) { ["one"]=> string(3) "111" ["two"]=> string(3) "222" ["three"]=> string(3) "333" } 

$arr4 = $myclass->get();
//array(3) { ["one"]=> string(3) "111" ["two"]=> string(3) "222" ["three"]=> string(3) "333" }
相关问题