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. Link to comment https://forums.phpfreaks.com/topic/170361-solved-array-key-by-alpha/ 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. Link to comment https://forums.phpfreaks.com/topic/170361-solved-array-key-by-alpha/#findComment-898663 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 ? Link to comment https://forums.phpfreaks.com/topic/170361-solved-array-key-by-alpha/#findComment-898669 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 ) Link to comment https://forums.phpfreaks.com/topic/170361-solved-array-key-by-alpha/#findComment-898683 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. Link to comment https://forums.phpfreaks.com/topic/170361-solved-array-key-by-alpha/#findComment-898692 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.