By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

array problem

P: 4
hi,

how to find in array that array keys contains special characters,punctuations only with alphabetic charaacters in each array key
ex: $array1=array("kish"=1,"kumar"=3,"nagesh"=5,"ok-fine"=8,"then-what"=3);
in the abve array example my requirement problem is :
in the above array data,
how to find which array key contains specail characters ex in above is :"ok-fine"
if key contains special characters,punctuations with alphabetic characters in each array key i find that
Aug 26 '08 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264

Markus
Expert 5K+
P: 6,050
Here's something I kicked up (please read up on regexp's as mentioned in Ro's post).

// comments are explanations
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /* We set an array with some offending array keys */
  4. $styles = array( 'summer' => 'skirts', 'summer-autumn' => 'browns and furr', 'autumn' => 'greys and hoodys', 'autumn-winter' => 'suits' );
  5.  
  6.  
  7. /* Next we define a regular expression. What this expression does is check for anything OTHER THAN a-z (both uppercase and lower) and 0-9 */
  8. $exp = '/[^a-zA-Z0-9]/';
  9.  
  10.  
  11. /* This is optional; it was just something to show the offending array keys */
  12. $outcasts = array();
  13.  
  14.  
  15. /* Now we use a foreach loop to traverse the array, setting the array key as $key and the value as $style. */
  16. foreach( $styles as $key => $style )
  17. {
  18.     /* Now for the 'preg_match' - these are used with regular expressions (like we're using. So, what we're saying is 'if preg_match returns true' (array key is bad!) ... */
  19.     if ( preg_match( $exp, $style ) )
  20.     {
  21.             /* Stick it in the outcasts array */
  22.         $outcasts[$key] = $style;
  23.     }
  24. }
  25.  
  26. echo '<pre>';
  27. print_r( $outcasts );
  28.  
  29. ?>
  30.  
Hope this is helpful!
Aug 26 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.