Zugzwangle Posted July 8, 2010 Share Posted July 8, 2010 Hi. I have an array named $parts: [0] => Event Rated game, 90m + 5s [1] => Site Room 1 [2] => Date 2010.07.05... and I simply want to use the first word of the array 'Value', as the 'Key'. so the new array would look like this: [Event] => Rated game, 90m + 5s [site] => Room 1 [Date] => 2010.07.05... Please help if you can. Quote Link to comment https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/ Share on other sites More sharing options...
bh Posted July 8, 2010 Share Posted July 8, 2010 Hi, Try something like this: $new_array = array(); foreach ($parts as $value) { $new_key = explode(' ', $value); $new_value = explode(' ', $value, 2); $new_array[$new_key[0]]= $new_value[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/#findComment-1083037 Share on other sites More sharing options...
Zugzwangle Posted July 8, 2010 Author Share Posted July 8, 2010 That kind of works. It results in: Array ( [Event] => Rated game, 90m + 5s [ Site] => Room 1 [ Date] => 2010.07.05 [ Round] => ? ... However there is a 'space' before Site for example. I need to manipulate the data. echo $new_array[' Site']; // outputs nothing.. How do I go about removing the white space if this is necessary? Quote Link to comment https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/#findComment-1083048 Share on other sites More sharing options...
bh Posted July 8, 2010 Share Posted July 8, 2010 example with substr function substr($string, 1); Quote Link to comment https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/#findComment-1083049 Share on other sites More sharing options...
Zugzwangle Posted July 8, 2010 Author Share Posted July 8, 2010 Oh, thanks for your code btw.. also this is solved... I used the itrim() function.. foreach ($parts as $value) { $new_key = explode(' ', $value); $new_value = explode(' ', $value, 2); $new_array[ltrim($new_key[0])]= $new_value[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/#findComment-1083053 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 Do something like: <?php $new_array = array(); foreach ($parts as $value) { list($key,$entry) = explode(' ', $value,2); $new_array[trim($key)]= trim($entry); } echo '<pre>' . print_r($new_array,true) . '</pre>'; ?> ken Quote Link to comment https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/#findComment-1083055 Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 Just for fun: foreach($parts as $value) { preg_match('/([\w]+) (.*)/', $value, $matches); $new[$matches[1]] = $matches[2]; } Quote Link to comment https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/#findComment-1083109 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.