Professional Web Applications Themes

Foreach that starts from 1 not 0? - PHP Development

Hello, I want to write smth that works like: foreach ($_REQUEST AS $key=>$val) echo "$key : $ val"; But it have to start from 1st element of $_REQUEST, not from 0 (like foreach does). I tried smth like: for ($i=1; $i<count($_REQUEST);$i+2) { $key=$_REQUEST[$i]; $val=$_REQUEST[$i+1]; but it doesn't seem to work. How to solve it? Regards, Talthen...

  1. #1

    Default Foreach that starts from 1 not 0?

    Hello,
    I want to write smth that works like:
    foreach ($_REQUEST AS $key=>$val) echo "$key : $ val";
    But it have to start from 1st element of $_REQUEST, not from 0 (like foreach
    does).

    I tried smth like:
    for ($i=1; $i<count($_REQUEST);$i+2)
    {
    $key=$_REQUEST[$i];
    $val=$_REQUEST[$i+1];

    but it doesn't seem to work. How to solve it?

    Regards,
    Talthen


    Guest

  2. #2

    Default Re: Foreach that starts from 1 not 0?

    On Sun, 31 Jul 2005 17:42:56 +0200, <pl> wrote:
     

    You probably want $i++, not $i+2 at the end.

    --
    Andy Hassall / <co.uk> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage ysis tool
    Andy Guest

  3. #3

    Default Re: Foreach that starts from 1 not 0?

    Uzytkownik "Andy Hassall" <co.uk> napisal w wiadomosci
    news:com... 
    >
    > You probably want $i++, not $i+2 at the end.[/ref]

    Well.. in fact i had $i=+2, but the "$i++" version doesn't work any better.
    I just don't know where are keys and where are values in this array.

    Regards,
    Talthen


    Guest

  4. #4

    Default Re: Foreach that starts from 1 not 0?

    On 2005-07-31 17-42-56 pl
    <<pl>> wrote: 

    Try:
    for ($l=sizeof($k=array_keys($_REQUEST)), $i=1; $i<$l; $i++)
    echo $k[$i], ': ', $_REQUEST[$k[$i]], "\n";

    Anyway your demand sounds strange, because you leave out one (more or less
    random) element in $_REQUEST. Maybe if you just post the reason, you need
    it for, we can provide a better solution.

    So far,
    Simon
    --
    Simon Stienen <http://slashlife.org/>
    "What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done."
    /Sherlock Holmes in A Study in Scarlet by Sir Arthur Conan Doyle/
    Simon Guest

  5. #5

    Default Re: Foreach that starts from 1 not 0?

    User "Simon Stienen" <slashlife.org> wrote in
    news:slashlife.org... 

    Pretty strange ;/
     

    Well... I have a form with some elements. First elements contains name of
    the file (and I access it with $_REQUEST['filename']), the rest are fields I
    want to write to that file. Formatting should look like:
    FormElementName : FormElementValue \n

    Regards,
    Talthen


    Guest

  6. #6

    Default Re: Foreach that starts from 1 not 0?

    I noticed that Message-ID: <dcj2at$c5g$news.tpi.pl> from
    <pl> contained the following:
     

    do a regular foreach and then just add

    if($key!="filename"){

    //do stuff

    }

    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  7. #7

    Default Re: Foreach that starts from 1 not 0?

    <pl> kirjoitti
    viestissä:dciria$r50$news.tpi.pl... 


    $firstelement = 0;
    foreach ($_REQUEST AS $key=>$val){
    if($firstelement++==0) continue;
    echo "$key : $val";
    }


    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>

    Soulman <com>


    Kimmo Guest

  8. #8

    Default Re: Foreach that starts from 1 not 0?

    "Kimmo Laine" : 

    Well, this looks nice, thanks.

    Regards,
    Talthen


    Guest

  9. #9

    Default Re: Foreach that starts from 1 not 0?

    On 2005-07-31 19-38-25 pl
    <<pl>> wrote: 
    >
    > Pretty strange ;/

    >
    > Well... I have a form with some elements. First elements contains name of
    > the file (and I access it with $_REQUEST['filename']), the rest are fields I
    > want to write to that file. Formatting should look like:
    > FormElementName : FormElementValue \n
    >
    > Regards,
    > Talthen[/ref]

    If you are sure that you do not need the filename anymore, you can use
    unset($_REQUEST['filename']); followed by your simple foreach.
    This should btw be faster than the solution Geoff Berrow posted, but his
    solution keeps the filename in $_REQUEST.
    My first thought was to copy $_REQUEST and doing the above with the copy,
    but this will be slower than Geoff Berrows solution again.

    Just choose what fits best to you.

    HTH,
    Simon
    --
    Simon Stienen <http://slashlife.org/>
    "What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done."
    /Sherlock Holmes in A Study in Scarlet by Sir Arthur Conan Doyle/
    Simon Guest

  10. #10

    Default Re: Foreach that starts from 1 not 0?

    I noticed that Message-ID: <slashlife.org> from
    Simon Stienen contained the following:
     

    Not that speed is likely to be an issue.

    --
    Geoff Berrow 0110001001101100010000000110
    001101101011011001000110111101100111001011
    100110001101101111001011100111010101101011
    Geoff Guest

  11. #11

    Default Re: Foreach that starts from 1 not 0?

    "Simon Stienen" <slashlife.org> ; 

    But will it be faster than
    =======
    $firstelement = 0;
    foreach ($_REQUEST AS $key=>$val){
    if($firstelement++==0) continue;
    echo "$key : $val";
    }
    =======
    by Kimmo Laine?

    Ragards,
    Talthen


    Guest

  12. #12

    Default Re: Foreach that starts from 1 not 0?

    <pl> kirjoitti
    viestissä:dcj55r$iq7$news.tpi.pl... 
    >
    > But will it be faster than
    > =======
    > $firstelement = 0;
    > foreach ($_REQUEST AS $key=>$val){
    > if($firstelement++==0) continue;
    > echo "$key : $val";
    > }
    > =======
    > by Kimmo Laine?
    >[/ref]

    Yes, it is faster than what I suggested. I'd say Simon's code is twice as
    fast, and more flexible. If filename for some reason wasn't the first
    element of the array (which really depends on the UA), Simon's code will
    still work while mine will fail, so I strongly suggest you choose that
    instead of mine.

    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>

    Soulman <com>


    Kimmo Guest

  13. #13

    Default Re: Foreach that starts from 1 not 0?

    "Kimmo Laine" <com> : 

    That's great, thank you guys. I'll use it.

    But now I have another problem: I have to join all fields from first to last
    separated with "," but withou "," at the end.
    I would rather like to not use ifs like in the example:

    $i=0;
    $Output='';
    foreach ($_REQUEST AS $key => $val)
    {
    $Output=$Output.$val;
    if ($i<count($_REQUEST)) $Output=$Output.",";
    }

    I don't know what is the name of the last item in $_REQUEST so I cannot copy
    it, and unset.
    Any suggestions?

    Regards,
    Talthen


    Guest

  14. #14

    Default Re: Foreach that starts from 1 not 0?

    <pl> kirjoitti
    viestissä:dcj8ap$eag$news.tpi.pl... 
    >
    > That's great, thank you guys. I'll use it.
    >
    > But now I have another problem: I have to join all fields from first to
    > last separated with "," but withou "," at the end.
    > I would rather like to not use ifs like in the example:
    >
    > $i=0;
    > $Output='';
    > foreach ($_REQUEST AS $key => $val)
    > {
    > $Output=$Output.$val;
    > if ($i<count($_REQUEST)) $Output=$Output.",";
    > }
    >
    > I don't know what is the name of the last item in $_REQUEST so I cannot
    > copy it, and unset.
    > Any suggestions?[/ref]

    implode() is just what you need. See http://fi2.php.net/implode

    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>

    Soulman <com>


    Kimmo Guest

  15. #15

    Default Re: Foreach that starts from 1 not 0?

    You probably meant
    for ($i=1; $i<count($_REQUEST);$i+=2) // Not $i+2

    Dae
     


    Daedalus.OS Guest

  16. #16

    Default Re: Foreach that starts from 1 not 0?

    On 2005-07-31 21-20-51 pl
    <<pl>> wrote: 
    >
    > That's great, thank you guys. I'll use it.
    >
    > But now I have another problem: I have to join all fields from first to last
    > separated with "," but withou "," at the end.
    > I would rather like to not use ifs like in the example:
    >
    > $i=0;
    > $Output='';
    > foreach ($_REQUEST AS $key => $val)
    > {
    > $Output=$Output.$val;
    > if ($i<count($_REQUEST)) $Output=$Output.",";
    > }
    >
    > I don't know what is the name of the last item in $_REQUEST so I cannot copy
    > it, and unset.
    > Any suggestions?
    >
    > Regards,
    > Talthen[/ref]

    Do it the other way around: Put a comma in front of the appended string, if
    if it's not the first element:
    $Output='';
    foreach ($_REQUEST AS $key => $val)
    $Output.=strlen($Output)?','.$val,$val;

    HTH,
    Simon
    --
    Simon Stienen <http://slashlife.org/>
    "What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done."
    /Sherlock Holmes in A Study in Scarlet by Sir Arthur Conan Doyle/
    Simon Guest

  17. #17

    Default Re: Foreach that starts from 1 not 0?

    On 2005-08-01 17-41-09 Simon Stienen <slashlife.org> wrote: 
    Has to be:
    $Output.=strlen($Output)?','.$val:$val;
    -------------------------------------^
    I'm sorry.

    Simon
    --
    Simon Stienen <http://slashlife.org/>
    "What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done."
    /Sherlock Holmes in A Study in Scarlet by Sir Arthur Conan Doyle/
    Simon Guest

  18. #18

    Default Re: Foreach that starts from 1 not 0?

    > implode() is just what you need. See http://fi2.php.net/implode

    Thanks,
    Talthen


    Guest

  19. #19

    Default Re: Foreach that starts from 1 not 0?

    >> $Output.=strlen($Output)?','.$val,$val; 

    Will check it, but implode looks so easy :]

    Thanks,
    Talthen


    Guest

  20. #20

    Default Re: Foreach that starts from 1 not 0?

    Simon Stienen wrote: 
    >
    > Has to be:
    > $Output.=strlen($Output)?','.$val:$val;
    > -------------------------------------^
    > I'm sorry.
    >
    > Simon[/ref]


    Will do if short tags are on
    Kleist Guest

Similar Threads

  1. Audio starts on click/stops/and finaly starts correctwith video
    By N steht f?r? in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: December 19th, 02:30 PM
  2. name starts with a-e
    By adamantics in forum Coldfusion - Getting Started
    Replies: 3
    Last Post: May 18th, 08:56 AM
  3. FHMX11.02 starts fine, then s l o w s down
    By plummerdesign in forum Macromedia Freehand
    Replies: 3
    Last Post: June 16th, 03:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139