note 51658 deleted from function.array-merge by levim

From: Date: Mon, 24 Feb 2014 22:53:26 +0000
Subject: note 51658 deleted from function.array-merge by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198676@lists.php.net to get a copy of this message
Note Submitter: jeff_1089 at yahoo dot com ---- Note that casting the array doesn't always work. For classes, it looks like the class information is lost. The print_r's below have different behavior. I would suggest creating an array, rather than casting in most situations. (ie. do it like $array2). class myclass { private $name; private $value; function myclass($n, $v) { $this->name=$n; $this->value=$v; } } $itema = new myclass("hair", "orange"); $itemb = new myclass("eyes", "green"); $itemc = new myclass("elbow", "dirty"); $items = array($itemb, $itemc); print "<BR>"; $array1 = array_merge((array)$itema, $items); $array2 = array_merge(array($itema), $items); print "<BR>"; print "<BR>"; print_r($array1); print "<BR>"; print "<BR>"; print_r($array2); Array ( [] => orange [0] => myclass Object ( [name:private] => eyes [value:private] => green ) [1] => myclass Object ( [name:private] => elbow [value:private] => dirty ) ) Array ( [0] => myclass Object ( [name:private] => hair [value:private] => orange ) [1] => myclass Object ( [name:private] => eyes [value:private] => green ) [2] => myclass Object ( [name:private] => elbow [value:private] => dirty ) )

« previous php.notes (#198676) next »