Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:45864 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65075 invoked from network); 22 Oct 2009 02:01:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2009 02:01:50 -0000 Authentication-Results: pb1.pair.com header.from=richardkmiller@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=richardkmiller@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.226 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: richardkmiller@gmail.com X-Host-Fingerprint: 209.85.220.226 mail-fx0-f226.google.com Received: from [209.85.220.226] ([209.85.220.226:39121] helo=mail-fx0-f226.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/E3-44649-D0DBFDA4 for ; Wed, 21 Oct 2009 22:01:49 -0400 Received: by fxm26 with SMTP id 26so11454048fxm.23 for ; Wed, 21 Oct 2009 19:01:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:subject:mime-version :content-type:from:in-reply-to:date:cc:content-transfer-encoding :message-id:references:to:x-mailer; bh=dX+IigCIgfwYqdag+jvFOdIub/9N87xvST0nQyqjbls=; b=Bkncabtrhn0ogE0X5JRP8S414xMhmLtX7M+em2XfB31IZTnvcQ/uv0OXSQcXleUPix iyzFwt+bPi2nW5wtMgzwpQd0amLTdt5Hxym+QlJtHGXc/DnrEbIOid2rb7Ez6UUTBtot pJlUlARbToTAbEaGD2QwkhZEfseXjxi/zjWb8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; b=OiCPGdzRXx7YV7fMLNI7RBz2xr02TESDcmoIP8ivzswDOSZSb2XLsc3ZVW72mspTsU 6LqzWlZ0KdUECYIet6bUoVRjmh9pC+WQNxngDV40KjYYZBZ8WJZxONJ9vDDksgzUou91 +1q1YTUUpVVID/kl+5AlH9kDFwt7MWS6LGzaw= Received: by 10.103.144.22 with SMTP id w22mr3746122mun.52.1256176905919; Wed, 21 Oct 2009 19:01:45 -0700 (PDT) Received: from ?192.168.1.110? ([76.8.194.242]) by mx.google.com with ESMTPS id b9sm1217676mug.9.2009.10.21.19.01.43 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 21 Oct 2009 19:01:44 -0700 (PDT) Mime-Version: 1.0 (Apple Message framework v1076) Content-Type: text/plain; charset=us-ascii; format=flowed; delsp=yes In-Reply-To: <4ADFB80B.9050207@hexon.cx> Date: Wed, 21 Oct 2009 20:01:40 -0600 Cc: internals@lists.php.net Content-Transfer-Encoding: 7bit Message-ID: <304A7A3B-9E8D-4BA5-8C01-511090D5F236@gmail.com> References: <84898698-728C-44ED-BF7A-E0E02C111F68@gmail.com> <4ADFB80B.9050207@hexon.cx> To: Jille Timmermans X-Mailer: Apple Mail (2.1076) Subject: Re: [PHP-DEV] bug when using foreach with references? From: richardkmiller@gmail.com (Richard K Miller) Hi Jill, >> > $items = array('apple', 'banana', 'carrot'); >> print_r($items); >> foreach ($items as &$item) { } > $item is now a reference to the last element of $items. >> print_r($items); >> foreach ($items as $item) { } > And here, the foreach loop writes to $item for each element: thus > assiging that value to the last element of $items. The last element of $items is 'carrot'. Why does it print apple, banana, banana? It sounds like you're saying that $item should be left with the value 'carrot', which makes sense, but why would the original array be modified? (These are empty foreach loops.) >> print_r($items); >> ?> >> >> // Output: >> Array >> ( >> [0] => apple >> [1] => banana >> [2] => carrot >> ) >> Array >> ( >> [0] => apple >> [1] => banana >> [2] => carrot >> ) >> Array >> ( >> [0] => apple >> [1] => banana >> [2] => banana >> ) >> >> Two bananas in the last set?! Not what I expected. > You can fix your bug by using 'unset($item);' after the second > foreach-loop. >