note 51658 added to function.array-merge

From: Date: Wed, 06 Apr 2005 20:17:12 +0000
Subject: note 51658 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-87617@lists.php.net to get a copy of this message
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 ) ) ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51658 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51658&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51658&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51658&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51658&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51658&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51658&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87617) next »