繁体   English   中英

将值附加到 class 中的数组?

[英]Appending values to array in a class?

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

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

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

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

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" }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM