2012-07-16 44 views
0

我试图在对象中创建元素的克隆并为克隆设置新名称。如何用另一个数组填充数组?

class My_Obj{ 
var $obj; 
var $obj_name; 
var clone;  //----int input to say how many clones are needed 
var clone_names;//----array input to make clone's name different from $obj 
function __construct($args = '') { 
    $defaults=array(
    /* codes to set up the default $obj */ 
) 
if ($this->clone >0){ 
    for ($i = 0; $i <= $this->clone; ++$i){ 
    $clone_obj[$i]['name'] = /* need to loop through array $clone_name to fill this */ 

} 
} 

/* other codes */ 
} 

例如,$ clone_names可以是数组('cat','dog','bird')。只要每个克隆获得一个名称,哪个订单并不重要。我想学习如何做到这一点。谢谢!

+0

这是相当困难的理解你的要求。请输入example * input data *和expected * output data *。 – deceze 2012-07-16 13:32:20

+0

强烈建议您采用[PHP5风格的类属性定义](http://us3.php.net/manual/en/language.oop5.php)。 PHP4样式'var'关键字不再使用。相反,你应该声明这些为“public”或“private”属性。 – 2012-07-16 13:32:42

+0

我也弄不清楚。而且你也缺少一些$符号。 – Pete 2012-07-16 13:33:25

回答

0

如果我正确理解你的问题,你想填充数组$clone_obj的值从$clone_names - 直到在$clone指定的克隆数?

如果是这样的情况下,这可能工作(我已经重写你使用的示例类):

class My_Obj { 
    public $clone = 0;   //----int input to say how many clones are needed 
    public $clone_names = array(); //----array input to make clone's name different from $obj 
    private $clone_obj = array(); // array to store cloned names in 

    function __construct($args = '') { 
     for ($i=0; $i<$this->clone; $i++) { 
      // copy the name in "$clone_names" to "$clone_obj", if it exists 
     $this->clone_obj[$i] = array(); 
      $this->clone_obj[$i]['name'] = (isset($this->clone_names[$i]) ? $this->clone_names[$i] : ''); 
     } 
    } 
} 
+0

谢谢,这正是我需要的。 – Jenny 2012-07-16 13:55:07

+0

太棒了,很高兴我能帮到你! – newfurniturey 2012-07-16 14:03:26