Nolongerused3921 Posted March 13, 2008 Share Posted March 13, 2008 I'm having a bit of a problem - I need to access an array entry (I.e., $array['key']['key2']) with a plain text string ($string = "[key][key2]"), but I'm not sure how to go about doing this... I've attempted to get the keys via regex, but that's not going so well and I'm going a bit insane, so I thought I'd get some help here. The regex, to give you an idea what I'm trying to do (I'd prefer not to use regex, if anyone can think of another way) : preg_match("/^([a-zA-Z0-9]+)(\[([a-zA-Z0-9]+)\])*/", $inputName, $sdf); Returns: cat[goes][meow] 0: Array ( [0] => cat[goes][meow] [1] => cat [2] => [meow] [3] => meow ) I've tried several variations, but they always result in only one key being matched and returned. Link to comment https://forums.phpfreaks.com/topic/96064-turning-string-into-array-indexer/ Share on other sites More sharing options...
kenrbnsn Posted March 13, 2008 Share Posted March 13, 2008 Here's one way of doing it: <?php $str = '[testkey1][testkey2]'; list($dk1,$dk2) = explode('][',$str); list(,$key1) = explode('[',$dk1); list($key2) = explode(']',$dk2); echo 'key1 = ' . $key1 . "<br>\n"; echo 'key2 = ' . $key2 . "<br>\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/96064-turning-string-into-array-indexer/#findComment-491806 Share on other sites More sharing options...
Nolongerused3921 Posted March 13, 2008 Author Share Posted March 13, 2008 Thanks for the quick reply, however I should have mentioned I had tried that, deciding not to use it due to issues with the speed of it. Sadly speed is broken down to: Functionless method (God I hope there's one)->Regex->sscanf->explode... The function will be called a lot, so I really wanna try to find something that's as faster than regex, but failing that - regex. Link to comment https://forums.phpfreaks.com/topic/96064-turning-string-into-array-indexer/#findComment-491809 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.