Re: how to ignore the first 2 items in this iteraction?



In article <jvd935dr2p6ie79dls54eddpkuespf7f0m@xxxxxxx>, gcox@xxxxxxxxxx
says...
On Sat, 13 Jun 2009 15:05:54 +0100, Geoff Berrow
<blthecat@xxxxxxxxxxx> wrote:

On Sat, 13 Jun 2009 08:24:11 -0400, Jerry Stuckle
<jstucklex@xxxxxxxxxxxxx> wrote:


foreach ($_POST as $value) {
$results .= $value . ",";
}
echo $results;
?>

How can I create $results so that the first 2 items are ignored?

Thanks

Geoff


Define "first two values". This is an associative array.

Indeed. What you need is an 'ignore' array.

<?php
//define items you want to ignore here
$ignore=array('first','second','submit');

$_POST['first'] = "1";
$_POST['second'] = "2";
$_POST['third'] = "fred";
$_POST['fourth'] = "jane";

foreach ($_POST as $key->$value) {
if(!in_array($key,$ignore)){
//using another array because we only want
//commas in between and not at the end
$results []= $value;
}

echo implode(",",$results);
?>

Untested.

Geoff

Thanks for your reply.

I am getting cannot access empty property error with

<?php
//define items you want to ignore here
$ignore=array('first','second','submit');

$_POST['first'] = "1";
$_POST['second'] = "2";
$_POST['third'] = "fred";
$_POST['fourth'] = "jane";

foreach ($_POST as $key->$value) {
if(!in_array($key,$ignore)){
//using another array because we only want
//commas in between and not at the end
$results []= $value;
}
}

echo implode(",",$results);
?>

any ideas?


Apologies if I'm completely wrong here, I'm fairly new to php and have
been lurking here for a couple of weeks to see what I can learn.

Anyway, should it be like this:

if(!in_array($key,$ignore)){
//using another array because we only want
//commas in between and not at the end
$results []= $value;
echo implode(",",$results);
}

Otherwise first time around nothing's been assigned to $results because
it would be 'ignored' in the if statement.

--
Pete Ives
Remove All_stRESS before sending me an email
.



Relevant Pages

  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: Dynamic Form
    ... customer will answer all 5 questions then press next. ... I have read the PHP manual pages. ... echo Age; ... only other thought is that for some reason, I can't access the array ...
    (comp.lang.php)
  • Re: Dynamic Form
    ... customer will answer all 5 questions then press next. ... I have read the PHP manual pages. ... echo Age; ... foreach { ...
    (comp.lang.php)
  • Re: Dynamic Form
    ... customer will answer all 5 questions then press next. ... I have read the PHP manual pages. ... echo Age; ... foreach { ...
    (comp.lang.php)