Anti-Moronic Posted August 27, 2009 Share Posted August 27, 2009 OK..I've searched and searched and have no idea if a function exists for this. I'm fairly beginner minded when it comes to advanced array functions. Here's my array: <?php array( array('key1'=>'1') array('key1'=>'2') array('key2'=>'3') ); What I want to produce is one array, like so: array('key1'=>'1,2','key2'=>'3'); ?> My head is absolutely spinning with this. Any help is *greatly* appreciated. Link to comment https://forums.phpfreaks.com/topic/172160-solved-multi-dimensional-array-arrange-all-values-by-same-keys/ Share on other sites More sharing options...
akitchin Posted August 27, 2009 Share Posted August 27, 2009 array('key1'=>'1,2','key2'=>'3'); do you need the values to be appended as one string with commas delimiting the values, or do you want those to be separate array values under the key 'key1'? Link to comment https://forums.phpfreaks.com/topic/172160-solved-multi-dimensional-array-arrange-all-values-by-same-keys/#findComment-907750 Share on other sites More sharing options...
lemmin Posted August 27, 2009 Share Posted August 27, 2009 I don't think there is a function in PHP that does that for you, but you can make a function like this: <?php $test = array( array('key1'=>'1'), array('key1'=>'2'), array('key2'=>'3') ); print_r(mergeKeys($test)); function mergeKeys($array) { $new = array(); foreach ($array as $t) { $cKey = key($t); if (!array_key_exists($cKey, $new)) $new = array_merge($new, array($cKey => current($t))); else $new[$cKey] .= "," . current($t); } return $new; } ?> Link to comment https://forums.phpfreaks.com/topic/172160-solved-multi-dimensional-array-arrange-all-values-by-same-keys/#findComment-907752 Share on other sites More sharing options...
Anti-Moronic Posted August 27, 2009 Author Share Posted August 27, 2009 Wow, thanks lemmin. Sadly, it doesn't seem to work for me. Input: Array ( [0] => Array ( [18266] => 1 ) [1] => Array ( [18266] => 2 ) [2] => Array ( [18266] => 3 ) [3] => Array ( [18266] => 4 ) [4] => Array ( [18266] => 5 ) [5] => Array ( [18289] => 6 ) [6] => Array ( [18289] => 7 ) [7] => Array ( [18289] => 8 ) [8] => Array ( [18289] => 9 ) ) Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) ------------------- Do you know where I'm going wrong or if the code needs tweaking? Thanks for you help! Link to comment https://forums.phpfreaks.com/topic/172160-solved-multi-dimensional-array-arrange-all-values-by-same-keys/#findComment-907761 Share on other sites More sharing options...
lemmin Posted August 27, 2009 Share Posted August 27, 2009 Oh, that's because array_merge won't duplicate numeric keys. I'm not sure why I used that in the first place. Try it like this: <?php $test = array( array('key1'=>'1'), array('key1'=>'2'), array('key2'=>'3') ); print_r(mergeKeys($test)); function mergeKeys($array) { $new = array(); foreach ($array as $t) { $cKey = key($t); if (!array_key_exists($cKey, $new)) $new[$cKey] = current($t); else $new[$cKey] .= "," . current($t); } return $new; } ?> Link to comment https://forums.phpfreaks.com/topic/172160-solved-multi-dimensional-array-arrange-all-values-by-same-keys/#findComment-907765 Share on other sites More sharing options...
Anti-Moronic Posted August 27, 2009 Author Share Posted August 27, 2009 Incredible. Thank you so much! Think I'll have to study these functions because I really don't know enough about arrays, evidently. Thanks again! Saved me hours of pulling my hair out! Link to comment https://forums.phpfreaks.com/topic/172160-solved-multi-dimensional-array-arrange-all-values-by-same-keys/#findComment-907768 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.