FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » array filtering question
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
array filtering question [message #178182] Mon, 21 May 2012 14:22 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
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~

I looked at array_keys, array_filter and others, but they seem to
all match the whole key string.

I can process the whole array myself, but I am hoping that with
all the array functions there might be one.

It would be nice if I could apply an array function and get back
an array with just those keys. (they are checkboxes so the value
is always "on", just their presence is enough information.)

bill
Re: array filtering question [message #178184 is a reply to message #178182] Mon, 21 May 2012 14:46 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/21/2012 10:22 AM, bill wrote:
> 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~
>
> I looked at array_keys, array_filter and others, but they seem to all
> match the whole key string.
>
> I can process the whole array myself, but I am hoping that with all the
> array functions there might be one.
>
> It would be nice if I could apply an array function and get back an
> array with just those keys. (they are checkboxes so the value is always
> "on", just their presence is enough information.)
>
> bill

There isn't a real easy way to do this other than going through the
whole array.

However, I think your design could be improved. Why have multiple
checkboxes with different names and the same value? Why not have all
the checkboxes with the same name and different values?

It would make processing much easier.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: array filtering question [message #178185 is a reply to message #178182] Mon, 21 May 2012 15:01 Go to previous messageGo to next message
Shake is currently offline  Shake
Messages: 40
Registered: May 2012
Karma: 0
Member
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.
}

greetings
Re: array filtering question [message #178186 is a reply to message #178184] Mon, 21 May 2012 15:18 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 5/21/2012 10:46 AM, Jerry Stuckle wrote:
> On 5/21/2012 10:22 AM, bill wrote:
>> 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~
>>
>> I looked at array_keys, array_filter and others, but they seem
>> to all
>> match the whole key string.
>>
>> I can process the whole array myself, but I am hoping that with
>> all the
>> array functions there might be one.
>>
>> It would be nice if I could apply an array function and get
>> back an
>> array with just those keys. (they are checkboxes so the value
>> is always
>> "on", just their presence is enough information.)
>>
>> bill
>
> There isn't a real easy way to do this other than going through
> the whole array.
>
> However, I think your design could be improved. Why have multiple
> checkboxes with different names and the same value? Why not have
> all the checkboxes with the same name and different values?
>
> It would make processing much easier.
>

Thanks Jerry,

There are major deletes (the ones I mentioned) and minor deletes
AND about another 200 fields with values. This is how it
appeared in the feeble thing I use for a brain.

It only took me 4 lines to create the array with the keys I need
in it. The key encodes the item to delete: apply substr to it
and I have the items to delete and the class of item they
represent (which leads to the file name...)

I suppose I could do it the way you suggested, but then I'd need
to get my mind wrapped around that design and this part of the
project is 90% complete (which of course means it is 90% incomplete).

Thanks for the reasurance.

bill
Re: array filtering question [message #178187 is a reply to message #178185] Mon, 21 May 2012 15:20 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 5/21/2012 11:01 AM, 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.
> }
>
> greetings

Nice idea.

bill
Re: array filtering question [message #178188 is a reply to message #178186] Mon, 21 May 2012 16:42 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/21/2012 11:18 AM, bill wrote:
> On 5/21/2012 10:46 AM, Jerry Stuckle wrote:
>> On 5/21/2012 10:22 AM, bill wrote:
>>> 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~
>>>
>>> I looked at array_keys, array_filter and others, but they seem
>>> to all
>>> match the whole key string.
>>>
>>> I can process the whole array myself, but I am hoping that with
>>> all the
>>> array functions there might be one.
>>>
>>> It would be nice if I could apply an array function and get
>>> back an
>>> array with just those keys. (they are checkboxes so the value
>>> is always
>>> "on", just their presence is enough information.)
>>>
>>> bill
>>
>> There isn't a real easy way to do this other than going through
>> the whole array.
>>
>> However, I think your design could be improved. Why have multiple
>> checkboxes with different names and the same value? Why not have
>> all the checkboxes with the same name and different values?
>>
>> It would make processing much easier.
>>
>
> Thanks Jerry,
>
> There are major deletes (the ones I mentioned) and minor deletes AND
> about another 200 fields with values. This is how it appeared in the
> feeble thing I use for a brain.
>
> It only took me 4 lines to create the array with the keys I need in it.
> The key encodes the item to delete: apply substr to it and I have the
> items to delete and the class of item they represent (which leads to the
> file name...)
>
> I suppose I could do it the way you suggested, but then I'd need to get
> my mind wrapped around that design and this part of the project is 90%
> complete (which of course means it is 90% incomplete).
>
> Thanks for the reasurance.
>
> bill
>

Actually, it shouldn't take any more to create the items with one key
and different ids. For instance, instead of creating something like:

<input type="checkbox" name="deleteField1" value="on">

You create

<input type="checkbox" name="delete[]" value="Field1">

You can call your major and minor delete checkboxes different names.

In this case, all the checkboxes with the name "delete[]" will be in
$_POST['delete'] array, i.e.

$_POST['delete'][0] might equal 'Field1',
$_POST['delete'][1] might equal 'Field3' (Field 2 NOT being checked)
and so on.

I find related checkboxes (i.e. similar processing for all) to be much
easier to handle this way.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: array filtering question [message #178189 is a reply to message #178185] Mon, 21 May 2012 17:26 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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
Re: array filtering question [message #178191 is a reply to message #178188] Mon, 21 May 2012 19:13 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 5/21/2012 12:42 PM, Jerry Stuckle wrote:
> On 5/21/2012 11:18 AM, bill wrote:
>> On 5/21/2012 10:46 AM, Jerry Stuckle wrote:
>>> On 5/21/2012 10:22 AM, bill wrote:
>>>> 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~
>>>>
>>>> I looked at array_keys, array_filter and others, but they seem
>>>> to all
>>>> match the whole key string.
>>>>
>>>> I can process the whole array myself, but I am hoping that with
>>>> all the
>>>> array functions there might be one.
>>>>
>>>> It would be nice if I could apply an array function and get
>>>> back an
>>>> array with just those keys. (they are checkboxes so the value
>>>> is always
>>>> "on", just their presence is enough information.)
>>>>
>>>> bill
>>>
>>> There isn't a real easy way to do this other than going through
>>> the whole array.
>>>
>>> However, I think your design could be improved. Why have multiple
>>> checkboxes with different names and the same value? Why not have
>>> all the checkboxes with the same name and different values?
>>>
>>> It would make processing much easier.
>>>
>>
>> Thanks Jerry,
>>
>> There are major deletes (the ones I mentioned) and minor
>> deletes AND
>> about another 200 fields with values. This is how it appeared
>> in the
>> feeble thing I use for a brain.
>>
>> It only took me 4 lines to create the array with the keys I
>> need in it.
>> The key encodes the item to delete: apply substr to it and I
>> have the
>> items to delete and the class of item they represent (which
>> leads to the
>> file name...)
>>
>> I suppose I could do it the way you suggested, but then I'd
>> need to get
>> my mind wrapped around that design and this part of the project
>> is 90%
>> complete (which of course means it is 90% incomplete).
>>
>> Thanks for the reasurance.
>>
>> bill
>>
>
> Actually, it shouldn't take any more to create the items with one
> key and different ids. For instance, instead of creating
> something like:
>
> <input type="checkbox" name="deleteField1" value="on">
>
> You create
>
> <input type="checkbox" name="delete[]" value="Field1">
>
> You can call your major and minor delete checkboxes different names.
>
> In this case, all the checkboxes with the name "delete[]" will be
> in $_POST['delete'] array, i.e.
>
> $_POST['delete'][0] might equal 'Field1',
> $_POST['delete'][1] might equal 'Field3' (Field 2 NOT being checked)
> and so on.
>
> I find related checkboxes (i.e. similar processing for all) to be
> much easier to handle this way.
>

Yes, that would have been. I didn't think about an array of
checkboxes. If it doesn't fall out of my brain I will do it that
way next time. Many thanks.

bill
Re: array filtering question [message #178193 is a reply to message #178189] Mon, 21 May 2012 19:19 Go to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 5/21/2012 1:26 PM, Thomas 'PointedEars' Lahn wrote:
> 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

Thanks for the education.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Latest project
Next Topic: array_walk always passing 0 as the parameter
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Oct 06 13:51:09 GMT 2024

Total time taken to generate the page: 0.02772 seconds