xiaix Posted August 15, 2009 Share Posted August 15, 2009 I have a string coming in from a database. For example: a0b34c7d0e2333 What I would like to do is build an end result keyed array with that string, like this: $dataArray = array("a" => "0", "b" => "34", "c" => "7", "d" => "0", "e" => "2333"); Since I don't have a valid delimiter for the explode function to work, I would like to know if I can use the letters (alpha) of the incoming string as a delimiter. What coding hurdles would I need to convert my incoming string into that keyed array? Is it even possible to do this? I thank you for your time and review. Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2009 Share Posted August 15, 2009 str_split. Always helps to check the manual first. Quote Link to comment Share on other sites More sharing options...
xiaix Posted August 15, 2009 Author Share Posted August 15, 2009 Thank you for your very quick reply. I could see str_split() working like I needed if my string were a one-to-one ratio, like: a4b2c5 But will str_split() still work the way I need it to if my string is: a4b24854c555 ? Quote Link to comment Share on other sites More sharing options...
Prismatic Posted August 15, 2009 Share Posted August 15, 2009 Here's one way, <?php $str = "a0b34c7d0e2333"; preg_match_all('/([a-z])([\d]+)/', $str, $part); $out = array_combine($part[1], $part[2]); print_r($out); ?> Output Array ( [a] => 0 [b] => 34 [c] => 7 [d] => 0 [e] => 2333 ) Quote Link to comment Share on other sites More sharing options...
xiaix Posted August 15, 2009 Author Share Posted August 15, 2009 Here's one way, <?php $str = "a0b34c7d0e2333"; preg_match_all('/([a-z])([\d]+)/', $str, $part); $out = array_combine($part[1], $part[2]); print_r($out); ?> Output Array ( [a] => 0 [b] => 34 [c] => 7 [d] => 0 [e] => 2333 ) You, my friend, are genius. Worked wonderfully. Thank you. Quote Link to comment 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.