GuitarGod Posted July 1, 2013 Share Posted July 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279756-regex-to-search-an-array-key/ Share on other sites More sharing options...
Psycho Posted July 1, 2013 Share Posted July 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279756-regex-to-search-an-array-key/#findComment-1438798 Share on other sites More sharing options...
Psycho Posted July 1, 2013 Share Posted July 1, 2013 (edited) Actually, look at the third user contribution that has a more efficient solution. Edited July 1, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/279756-regex-to-search-an-array-key/#findComment-1438809 Share on other sites More sharing options...
.josh Posted July 16, 2013 Share Posted July 16, 2013 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 Report a bug 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. Quote Link to comment https://forums.phpfreaks.com/topic/279756-regex-to-search-an-array-key/#findComment-1440852 Share on other sites More sharing options...
Psycho Posted July 16, 2013 Share Posted July 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279756-regex-to-search-an-array-key/#findComment-1440854 Share on other sites More sharing options...
.josh Posted July 16, 2013 Share Posted July 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279756-regex-to-search-an-array-key/#findComment-1440924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.