Re: array filtering question



Shake wrote:

El 21/05/2012 16:22, bill escribió:
I have a POST array with about 200 elements.
There MAY be one or more key elements that start with deleteF~
(for example deleteF~EAP, deleteF~Lowfee)

What I want to know is if any array element begins with deleteF~

Perhaps you can use multidimensional array here.

If you have something like:

<input type="checkbox" name="item_1" value="x" />
<input type="checkbox" name="[deleteF~][item_2]" value="x" />
<input type="checkbox" name="item_3" value="x" />
<input type="checkbox" name="[deleteF~][item_4]" value="x" />

Perhaps if better to change to:

Then the POST will be:

$_POST['item_1] = 'X';
$_POST['item_3] = 'X';
$_POST['deleteF~']['item_2] = 'X';
$_POST['deleteF~']['item_4] = 'X';

And you can do:

if(isset($_POST['deleteF~'])) foreach($_POST['deleteF~'] as $Item) {

// Do things with this item.
}

However, if for some reason the markup cannot be changed, the solution still
is rather simple:

$matching_keys = array_filter(array_keys($_POST),
function ($key) {
return preg_match('/^deleteF~/', $key);
});

if ($matching_keys)
{
/* … */
}

<http://php.net/array_keys>
<http://php.net/preg_match>
<http://php.net/array_filter>


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
.



Relevant Pages

  • Re: array filtering question
    ... There MAY be one or more key elements that start with deleteF~ ... What I want to know is if any array element begins with deleteF~ ... checkboxes with different names and the same value? ...
    (comp.lang.php)
  • Re: array filtering question
    ... On 5/21/2012 10:22 AM, bill wrote: ... There MAY be one or more key elements that start with deleteF~ ... What I want to know is if any array element begins with deleteF~ ... I can process the whole array myself, but I am hoping that with all the ...
    (comp.lang.php)
  • Re: array filtering question
    ... There MAY be one or more key elements that start with deleteF~ ... What I want to know is if any array element begins with deleteF~ ... checkboxes with different names and the same value? ...
    (comp.lang.php)
  • Re: array filtering question
    ... On 5/21/2012 11:01 AM, Shake wrote: ... There MAY be one or more key elements that start with deleteF~ ... What I want to know is if any array element begins with deleteF~ ...
    (comp.lang.php)
  • Re: array filtering question
    ... On 5/21/2012 11:18 AM, bill wrote: ... What I want to know is if any array element begins with deleteF~ ... checkboxes with different names and the same value? ...
    (comp.lang.php)