viviosoft Posted April 7, 2012 Share Posted April 7, 2012 Hello All - I have the following array: array 0 => array 'farmName' => string 'Hallinan' (length= 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 1 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 2 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 3 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 4 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 5 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 6 => array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 7 => array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 8 array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) The data comes from a flat file that has (as you can see) many of the same names like Home and Holt. Is there a way to extract the first Holt and Home from the array and put those values in a new array? So my new array would look something like: array 0 => array 'farmName' => string 'Hallinan' (length= 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 1 => array 'farmName' => string 'Holt' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) 2 => array 'farmName' => string 'Home' (length=4) 'farmInsNum' => string '' (length=0) 'farmFSA' => string '' (length=0) I look through php.net and really couldn't find a function that could handle what I was after. I'm sure I'd have to use a combination of each() and reset() or maybe not? Any help would be very welcome. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260509-array-problem/ Share on other sites More sharing options...
viviosoft Posted April 7, 2012 Author Share Posted April 7, 2012 I think this will do what I'm after. If there's a cleaner way or another approach, by all means please... $dup_name = array(); foreach ($farmData as $farm) { $single_name = array_merge($dup_name, explode(",", $farm['farmName'])); } var_dump(array_unique($single_name)); Quote Link to comment https://forums.phpfreaks.com/topic/260509-array-problem/#findComment-1335219 Share on other sites More sharing options...
viviosoft Posted April 7, 2012 Author Share Posted April 7, 2012 It would be nice if I could retain the key names in the initial array. Here's the output now. The method I'm using "works" but I loose the other keys in the array. <?php $dup_name = array(); foreach ($farmData as $farm) { $single_name = array_merge($dup_name, explode(",", $farm['farmName'])); } var_dump(array_unique($single_name)); ?> Output: <?php array 0 => string 'Hallinan' (length= 1 => string 'Holt' (length=4) 6 => string 'Home' (length=4) 9 => string 'Kenyon' (length=6) 19 => string 'Lane' (length=4) 21 => string 'Leach' (length=5) 23 => string 'Robinson' (length= 24 => string 'Shorey' (length=6) 26 => string 'Stream' (length=6) ?> Quote Link to comment https://forums.phpfreaks.com/topic/260509-array-problem/#findComment-1335223 Share on other sites More sharing options...
viviosoft Posted April 7, 2012 Author Share Posted April 7, 2012 Wow! I guess I should have waited to post . I figured out a solution. Well, I didn't I found the solution on php.net. Here's what I found for those that might have the same problem. <?php var_dump(remove_duplicate($farmData, 'farmName')); function remove_duplicate($array, $field) { foreach ($array as $sub) $cmp[] = $sub[$field]; $unique = array_unique($cmp); foreach ($unique as $k => $rien) $new[] = $array[$k]; return $new; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260509-array-problem/#findComment-1335227 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.