簡體   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