matthewtbaker Posted October 2, 2012 Share Posted October 2, 2012 Hi, I've been searching high and low for this answer all day and still no luck, so I thought I'd reach out to a fellow PHPer. If you can help I'd be most grateful! Ok here's the problem I'm trying to solve: I have a string value of; show_title=yes,show_intro=no,show_category=yes,show_print_icon=no,show_email_icon=yes I would like to split the string value in two places (removing the symbol at the same time), at the comma and at the equals. Then I'd like to put the values into an array naming each entry by the value entered before the equals symbol. Like so, show_title => yes show_intro => no show_category => yes show_print => no show_email_icon => no I'm hoping from doing this I can then easily refer to the setting I need later in my script. if ($array[show_print] == true) { you get the idea... } Thank for your time again! Matt Quote Link to comment https://forums.phpfreaks.com/topic/269005-split-string-and-create-array-with-named-values/ Share on other sites More sharing options...
Pikachu2000 Posted October 2, 2012 Share Posted October 2, 2012 (edited) Probably the easiest thing to do would be str_replace the commas with ampersands, then use parse_str with the optional second parameter to parse it into an associative array. Edited October 2, 2012 by Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/269005-split-string-and-create-array-with-named-values/#findComment-1382268 Share on other sites More sharing options...
Barand Posted October 2, 2012 Share Posted October 2, 2012 <?php $str = "show_title=yes,show_intro=no,show_category=yes,show_print_icon=no,show_email_icon=yes"; $str = str_replace(',', "\n", $str); $x = parse_ini_string($str); echo '<pre>'.print_r($x, 1).'</pre>'; ?> Result: Array ( [show_title] => 1 [show_intro] => [show_category] => 1 [show_print_icon] => [show_email_icon] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/269005-split-string-and-create-array-with-named-values/#findComment-1382269 Share on other sites More sharing options...
matthewtbaker Posted October 2, 2012 Author Share Posted October 2, 2012 (edited) Probably the easiest thing to do would be str_replace the commas with ampersands, then use parse_str with the optional second parameter to parse it into an associative array. Thank you for the speedy reply Pikachu2000! It tried this approach and works like a charm. For those coming across this, the solution looked like the below. $strAttributesNew = str_replace(",","&",$strAttributes); parse_str($strAttributesNew,$myNewArray); print_r($myNewArray); //just to check all is working ok Edited October 2, 2012 by matthewtbaker Quote Link to comment https://forums.phpfreaks.com/topic/269005-split-string-and-create-array-with-named-values/#findComment-1382270 Share on other sites More sharing options...
Psycho Posted October 2, 2012 Share Posted October 2, 2012 -- marking solved based upon OPs last response -- Quote Link to comment https://forums.phpfreaks.com/topic/269005-split-string-and-create-array-with-named-values/#findComment-1382272 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.