rbrown Posted February 23, 2008 Share Posted February 23, 2008 I have: lscr|30,lsct|d,lsmcr|30,lsmct|d,msmcr|30,msmct|d,keancr|30,keanct|d,kswl|yes And I want it to be returned as an array named returned with the first value as the key and the second value as the value: $returned['lscr'] would equal 30 $returned['lsct'] would equal d etc... I'm just being stupid today and can't figure it out. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/92563-string-to-array-problem/ Share on other sites More sharing options...
sasa Posted February 23, 2008 Share Posted February 23, 2008 try <?php $a = 'lscr|30,lsct|d,lsmcr|30,lsmct|d,msmcr|30,msmct|d,keancr|30,keanct|d,kswl|yes'; foreach (explode(',',$a) as $tmp){ $tmp = explode('|', $tmp); $returned[$tmp[0]] = $tmp[1]; } print_r($returned); ?> Quote Link to comment https://forums.phpfreaks.com/topic/92563-string-to-array-problem/#findComment-474359 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 this should work... <?php $orig_str = 'lscr|30,lsct|d,lsmcr|30,lsmct|d,msmcr|30,msmct|d,keancr|30,keanct|d,kswl|yes'; $keyval_array = explode(',', $orig_str); foreach($keyval_array as $keyval){ $ele_arr = explode('|', $keyval); $returned[$ele_arr[0]] = $ele_arr[1]; } print_r($returned); ?> Quote Link to comment https://forums.phpfreaks.com/topic/92563-string-to-array-problem/#findComment-474362 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Share Posted February 23, 2008 nice code, sasa Quote Link to comment https://forums.phpfreaks.com/topic/92563-string-to-array-problem/#findComment-474363 Share on other sites More sharing options...
rbrown Posted February 23, 2008 Author Share Posted February 23, 2008 Beautiful... Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/92563-string-to-array-problem/#findComment-474381 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.