Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5342

Re: Doubt regarding an array of references...

From "M. Strobel" <sorry_no_mail_here@nowhere.dee>
Newsgroups comp.lang.php
Subject Re: Doubt regarding an array of references...
Date 2012-04-04 10:48 +0200
Message-ID <9u2g7aF5jgU1@mid.uni-berlin.de> (permalink)
References <jlcjem$rrb$1@dont-email.me> <jleeb1$a6i$1@dont-email.me> <9u05rvFhmrU1@mid.uni-berlin.de> <jleov5$9la$1@dont-email.me>

Show all headers | View raw


Am 03.04.2012 14:03, schrieb Jerry Stuckle:
> On 4/3/2012 7:39 AM, M. Strobel wrote:
>>
>> My 2¢:
>>
>> The problem with the counted for-loop is tedious writing, and off-by-one error.
>>
> 
> No error if you remember what you're doing.  They can be tedious, but allow more
> control over the loop.  The real problem here is the indicies must be integers and
> sequential.
> 
> OTOH, foreach() loops process the elements in the order they were added to the array,
> which is not necessarily numeric order, i.e.
> 
> $a = array(3=>4, 2=>5, 1=>6);
> foreach ($a as $v)
>   echo "$v\n";
> 
> prints
> 4
> 5
> 6
> 
> Whereas
> for ($i = 1; i < 4; i++)
>   echo "$v\n";
> 
> prints
> 6
> 4
> 5
> 
>> The problem with the foreach-loop is: don't forget to use the key when you want to
>> change values!
>>
>> This works:
>>
>> #--------------------------------
>> strobel@s114-intel:~>  php -a
>> Interactive shell
>>
>> php>  $a = array(1,2,3);
>> php>  foreach ($a as $k=>$v) { $a[$k]++; }
>> php>  var_dump($a);
>> array(3) {
>>    [0]=>
>>    int(2)
>>    [1]=>
>>    int(3)
>>    [2]=>
>>    int(4)
>> }
>> php>  foreach ($a as $k=>$v) { $a[$k]*=4; }
>> php>  var_dump($a);
>> array(3) {
>>    [0]=>
>>    int(8)
>>    [1]=>
>>    int(12)
>>    [2]=>
>>    int(16)
>> }
>> php>
>> #--------------------------
>>
>> I see references in PHP as "a can of worms", in PHP5 they are only necessary in very
>> special cases.
>>
>> /Str.
>>
> 
> References are very handy - and prevent the need for such gyrations as you're going
> through.
> 
> I find this much easier to understand:
> 
> $a = array(1,2,3);
> foreach ($a as &$v) {
>   $v++;
> }
> 

more comments:

I wondered if your version is faster. It is a little slower:

Records read: 3.917.116, in seconds: 4.1342887878418
histogram_foreach in seconds: 1.2060449123383    "foreach ($a as $k=>$v)"
histogram_arraywalk in seconds: 2.3749718666077
histogram_whileeach in seconds: 2.9545080661774
histogram_foreach_r in seconds: 1.5193150043488  "foreach ($a as &$v)"

I am careful with generalizing benchmark results, this at least shows my "plain"
version has no performance penalty.

Anyway, my advice is: optimize for clearness && cleannes of code first.

/Str.

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Doubt regarding an array of references... "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-02 11:47 -0430
  Re: Doubt regarding an array of references... "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-04-03 11:02 +0200
    Re: Doubt regarding an array of references... "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-04-03 13:39 +0200
      Re: Doubt regarding an array of references... Jerry Stuckle <jstucklex@attglobal.net> - 2012-04-03 08:03 -0400
        Re: Doubt regarding an array of references... "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-04-03 14:15 +0200
        Re: Doubt regarding an array of references... "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-04-04 10:48 +0200

csiph-web