Jump to content

Recommended Posts

Hi all,

 

I have an array called 'menu', which when put through print_r, currently outputs:

Array ( [4_00] => Array ( [0] => 1 ), [3_00] => Array ( [0] => 1 ), [7_00] => Array ( [0] => 1 ) )

I want to create an expression that will search this array for any keys beginning with '4_'. I have tried with this:

$matches = preg_grep( '/^4_/', $menu );

But that doesn't seem to work.

 

Any help?

 

Much appreciated :happy-04:

Link to comment
https://forums.phpfreaks.com/topic/279756-regex-to-search-an-array-key/
Share on other sites

I think the function is poorly defined in the manual. The function preg_grep() returns an array of the keys where the VALUE of the array matches the regular expression. Take a look at the second user note in the manual where someone submitted a function to do a search using the keys.

 

http://php.net/manual/en/function.preg-grep.php

  • 2 weeks later...

I think the function is poorly defined in the manual. The function preg_grep() returns an array of the keys where the VALUE of the array matches the regular expression. Take a look at the second user note in the manual where someone submitted a function to do a search using the keys.

 

http://php.net/manual/en/function.preg-grep.php

 

The manual says:

 

preg_grep — Return array entries that match the pattern

notes-reject.gif Description
array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

Returns the array consisting of the elements of the input array that match the given pattern.

How is this poorly defined?  Not entirely useful? Sure.  Poorly defined? Nope. 

How is this poorly defined?  Not entirely useful? Sure.  Poorly defined? Nope. 

 

Isn't that just splitting hairs? I will say I should have stated it was poorly "described". Nonetheless, if a description (definition) is not useful then isn't it, by its very purpose, poorly described (defined)? To be fair, the description for the Return Values is absolutely clear

 

 

Returns an array indexed using the keys from the input array.

 

So any misunderstanding is the fault of the reader - not the manual. But, as we all know, people don't always read the manual - and when they do - rarely read it in its entirety. Based upon that knowledge, it is my opinion that the description could be worded to provide a more intuitive description of what the function does.

No, I am not trying to split hairs; my contention is that it IS worded fair enough. My concession is that it has limited use as an actual function - IMO a function that will return the keys, as shown in the comments you pointed out, would be more useful.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.