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


Groups > comp.lang.php > #12305 > unrolled thread

foreach problem part two

Started byrichard <noreply@example.com>
First post2013-12-19 13:41 -0500
Last post2013-12-20 18:37 +0000
Articles 6 on this page of 26 — 13 participants

Back to article view | Back to comp.lang.php


Contents

  foreach problem part two richard <noreply@example.com> - 2013-12-19 13:41 -0500
    Re: foreach problem part two Christoph Michael Becker <cmbecker69@arcor.de> - 2013-12-19 20:00 +0100
      Re: foreach problem part two Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-19 19:16 +0000
        Re: foreach problem part two The Natural Philosopher <tnp@invalid.invalid> - 2013-12-19 19:21 +0000
      Re: foreach problem part two The Natural Philosopher <tnp@invalid.invalid> - 2013-12-19 19:20 +0000
      Re: foreach problem part two Evan Platt <evan@theobvious.espphotography.com.invalid> - 2013-12-19 23:07 -0800
        Re: foreach problem part two Scott Johnson <noonehome@chalupasworld.com> - 2013-12-20 00:59 -0800
    fixed!  (was: foreach problem part two) richard <noreply@example.com> - 2013-12-19 16:07 -0500
      Re: fixed!  (was: foreach problem part two) Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-20 18:48 +0000
    Re: foreach problem part two Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-12-19 22:34 +0000
      Re: foreach problem part two richard <noreply@example.com> - 2013-12-19 18:39 -0500
        Re: foreach problem part two Jerry Stuckle <jstucklex@attglobal.net> - 2013-12-19 19:51 -0500
        Re: foreach problem part two Christoph Michael Becker <cmbecker69@arcor.de> - 2013-12-20 02:09 +0100
          Re: foreach problem part two richard <noreply@example.com> - 2013-12-19 23:05 -0500
            Re: foreach problem part two Richard Damon <Richard@Damon-Family.org> - 2013-12-19 23:37 -0500
              Re: foreach problem part two Norman Peelman <npeelman@cfl.rr.com> - 2013-12-20 07:14 -0500
                Re: foreach problem part two Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-20 16:35 +0100
            Re: foreach problem part two Norman Peelman <npeelman@cfl.rr.com> - 2013-12-20 07:11 -0500
            Re: foreach problem part two Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-12-20 13:17 +0000
            Re: foreach problem part two Evan Platt <evan@theobvious.espphotography.com.invalid> - 2013-12-20 09:14 -0800
            Re: foreach problem part two Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-20 18:52 +0000
          Re: foreach problem part two John Smith <johnsmithjsm@live.com> - 2014-01-06 23:13 -0500
            Re: foreach problem part two Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-07 10:07 +0100
        Re: foreach problem part two Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-12-20 03:11 +0000
        Re: foreach problem part two Arno Welzel <usenet@arnowelzel.de> - 2013-12-20 07:52 +0100
        Re: foreach problem part two Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-20 18:37 +0000

Page 2 of 2 — ← Prev page 1 [2]


#12338

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-12-20 18:52 +0000
Message-ID<l923l7$d0q$3@dont-email.me>
In reply to#12321
On Thu, 19 Dec 2013 23:05:59 -0500, richard wrote:

> On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker wrote:
> 
>> richard wrote:
>> 
>>> There is a flaw in the works that is not discussed.
>>> That being, it won't work with brackted arrays.
>>> Works fine with standard arrays.
>> 
>> Nonsense.  PHP has only one array type.[1]

> As you and others so kindly keep repeating, RTFM!
> 
> $array[0][0]="data"
> 
> Is 100% valid!

Yes, it's an array of arrays. But it's still just an array of things, 
even if each thing is an array.

PHP has a single array type. An array is a collection of things. The 
things can be arrays, ints, strings, classes, etc, but the containing 
array is just an array of things.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#12560

FromJohn Smith <johnsmithjsm@live.com>
Date2014-01-06 23:13 -0500
Message-ID<pmvmc9tdoauppv1gfk5t27ti48ctqcjko1@4ax.com>
In reply to#12318
On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker
<cmbecker69@arcor.de> wrote:

>richard wrote:
>
>> There is a flaw in the works that is not discussed.
>> That being, it won't work with brackted arrays.
>> Works fine with standard arrays.
>
>Nonsense.  PHP has only one array type.[1]
>
>> While (){}. however works and could care less which is used.
>
>Assume, you have an array:
>
>  $array = array('one', 'two', 'three');
>
>Now you want to echo its elements in order.  What is simpler and more
>readable?
>
>  $i = 0;
>  while ($i < count($array)) {
>    echo $array[$i];
>  }
>
>or
>
>  foreach ($array as $element) {
>    echo $element;
>  }
>
>Additionally, the foreach loop is most likely faster.
>
>[1] <http://www.php.net/manual/en/language.types.array.php>

At the first example you don't have to increment the $i?

John

[toc] | [prev] | [next] | [standalone]


#12562

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-01-07 10:07 +0100
Message-ID<52cbc3ec$0$9522$9b4e6d93@newsspool1.arcor-online.net>
In reply to#12560
John Smith wrote:

> On Fri, 20 Dec 2013 02:09:46 +0100, Christoph Michael Becker
> <cmbecker69@arcor.de> wrote:
> 
>> Assume, you have an array:
>>
>>  $array = array('one', 'two', 'three');
>>
>> Now you want to echo its elements in order.  What is simpler and more
>> readable?
>>
>>  $i = 0;
>>  while ($i < count($array)) {
>>    echo $array[$i];
>>  }
> 
> At the first example you don't have to increment the $i?

Oops!  Of course you'd have to:

  $i = 0;
  while ($i < count($array)) {
    echo $array[$i];
    $i++;
  }

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#12320

FromDoug Miller <doug_at_milmac_dot_com@example.com>
Date2013-12-20 03:11 +0000
Message-ID<XnsA29BE189A82E8dougmilmaccom@78.46.70.116>
In reply to#12316
richard <noreply@example.com> wrote in news:y1yigdragnyw$.c2kv48q50osj.dlg@
40tude.net:

> On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
> 
>> richard <noreply@example.com> wrote in news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
>> 40tude.net:
>> 
>>>     <?php
>>>        foreach ($aname as $item){
>>>           echo $aname[$item][1];
>>>           echo "  (".$aname[$item][2].")";
>>>         }
>>>      ?>
>>> 
>>> I decided to create a second array that holds only the artist and number of
>>> records.
>>> So why am I getting "invalid argument" with this?
>> 
>> Because you don't understand how foreach() works. RTFM.
> 
> I did.

Then read it again, because you obviously didn't understand it first time through.

> There is a flaw in the works that is not discussed.

No, there isn't. The only flaw here is your failure to understand the way foreach() works. 
RTFM.

> That being, it won't work with brackted arrays.

That's *not* where the problem is in your code. RTFM.

[toc] | [prev] | [next] | [standalone]


#12323

FromArno Welzel <usenet@arnowelzel.de>
Date2013-12-20 07:52 +0100
Message-ID<52B3E919.8020109@arnowelzel.de>
In reply to#12316
richard, 2013-12-20 00:39:

> On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
> 
>> richard <noreply@example.com> wrote in news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
>> 40tude.net:
>>
>>>     <?php
>>>        foreach ($aname as $item){
>>>           echo $aname[$item][1];
>>>           echo "  (".$aname[$item][2].")";
>>>         }
>>>      ?>
>>>
>>> I decided to create a second array that holds only the artist and number of
>>> records.
>>> So why am I getting "invalid argument" with this?
>>
>> Because you don't understand how foreach() works. RTFM.
> 
> I did.
> There is a flaw in the works that is not discussed.
> That being, it won't work with brackted arrays.
> Works fine with standard arrays.

Sigh...

There is no thing like "bracketed arrays" or "standard arrays". This is
called one-dimensional and multi-dimensional. And of course foreach()
works fine with every kind of array.

<?php
$myarray = array(
  array( 'Apple', 1 ),
  array( 'Pie', 2 )
);

foreach($myarray as $item)
{
  echo $item[0] . ' - ' . $item[1] . '<br />';
}
?>

You just don't understand it - as usual.

> While (){}. however works and could care less which is used.

As usual. while() needs more code, is slower and is prone to errors.

Really - software development is not your thing. It seems you are
mentally not capable of understanding the principles.


-- 
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de

[toc] | [prev] | [next] | [standalone]


#12336

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-12-20 18:37 +0000
Message-ID<l922pv$d0q$1@dont-email.me>
In reply to#12316
On Thu, 19 Dec 2013 18:39:28 -0500, richard wrote:

> On Thu, 19 Dec 2013 22:34:24 +0000 (UTC), Doug Miller wrote:
> 
>> richard <noreply@example.com> wrote in
>> news:oib4w8z2sr5y$.15xxzxjds0xb0$.dlg@
>> 40tude.net:
>> 
>>>     <?php
>>>        foreach ($aname as $item){
>>>           echo $aname[$item][1];
>>>           echo "  (".$aname[$item][2].")";
>>>         }
>>>      ?>
>>> 
>>> I decided to create a second array that holds only the artist and
>>> number of records.
>>> So why am I getting "invalid argument" with this?
>> 
>> Because you don't understand how foreach() works. RTFM.
> 
> I did.
> There is a flaw in the works that is not discussed.
> That being, it won't work with brackted arrays.
> Works fine with standard arrays.

Foreach works with all arrays. At the foreach level, all arrays are one-
dimensional, in that foreach works with the topmost dimension of the 
array being processed.

The only flaw is your lack of understanding of how foreach works.

If you read and comprehended the manual entry for foreach, you would 
realise this.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.php


csiph-web