johnsmith153 Posted October 9, 2008 Share Posted October 9, 2008 if(array_unique($array[3])){echo "yes";} would be deal. I need to check if a value is unique in that array. $array[0]="jim"; $array[1]="paul"; $array[2]="dave"; $array[3]="jim"; so array 3 is NOT unique. I know array_unique removes all duplicates, so this cant be it Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 9, 2008 Share Posted October 9, 2008 You could use array_keys and count the number of items in the returned array. Alternatively, see the post a couple down from yours Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted October 9, 2008 Author Share Posted October 9, 2008 I dont think any of this will do it. I need to CREATE unique values for all matching arrays, so, if I start $array[0]="Jim Smith"; $array[1]="Dave Brown"; $array[2]="Dennis Brown"; $array[3]="Steve Smith"; I need to return the surname, but if not unique, I need to append the first letter of name, then keep checking to ensure unique So I would then end with: $array[0]="J.Smith"; $array[1]="Da.Brown"; $array[2]="De.Brown"; $array[3]="S.Smith"; Any ideas? Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted October 9, 2008 Author Share Posted October 9, 2008 Any ideas? Quote Link to comment Share on other sites More sharing options...
Slip Posted October 9, 2008 Share Posted October 9, 2008 There may be a function out there to work this out but a long way round would be looping through the array to create a string then testing the string with substr_count() to return an integer of matching occurances. Then it would be a simple case of testing the value of the integer using an if statement. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 9, 2008 Share Posted October 9, 2008 This may be expensive, but could you possibly set each name to its own array containing first ans last names array( array('first_name' => 'jim', 'last_name' => 'smith') ); then use this class to order by last name (http://www.phpfreaks.com/forums/index.php/topic,220273.0.html) then loop through that returned array and see if the current iteration's last_name field is equal to the previous iteration's. If it is, appened the second letter of the first name, if not, just the first letter. Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted October 9, 2008 Author Share Posted October 9, 2008 Thanks for the ideas. Emehrkay has a possible idea, but it does seem very long winded. Just to help, there will never be any more than 14 different names to consider. This may make it easier to do this. Either way, there must be a better way to do this. Any more ideas are appreciated? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 9, 2008 Share Posted October 9, 2008 This should be a php4 safe version (your sig says php4) of that sort class <?php /** * */ class Utilities_Array_Sort{ /** * @param _array array array of data to be sorted * @access private */ var $_array; /** * @param _sort_key String the key to sort the array by * @access private */ var $_sort_key; /** * Class Constructor * * @access public * @return null */ function Utilities_Array_Sort(array $array = array(), $sort_key = null){ $this->_array = $array; $this->_sort_key = $sort_key; } /** * sorts the array * * @param $direction String Direction that you would like to sort the array by * @access public * @return RETURN TYPE */ function sort($direction = 'ascending'){ switch($direction){ case 'asc': case 'ascending': usort($this->_array, array('Utilities_Array_Sort', 'ascending')); break; case 'desc': case 'descending': usort($this->_array, array('Utilities_Array_Sort', 'descending')); break; } return $this->_array; } /** * sets a new array * * @access public * @return instance of Utilities_Array_Sort class */ function setArray(array $array){ $this->_array = $array; return $this; } /** * sets a new sort by key * * @access public * @return instance of Utilities_Array_Sort class */ function setSortKey($sort_key = false){ if($sort_key) $this->_sort_key = $sort_key; return $this; } /** * sorts the array in ascending order * * @access private * @return int */ function ascending($a, $b){ return strcmp($a[$this->_sort_key], $b[$this->_sort_key]); } /** * sorts the array in descending order * * @access private * @return int */ function descending($a, $b){ return strcmp($a[$this->_sort_key], $b[$this->_sort_key]) * -1; } } ?> Quote Link to comment Share on other sites More sharing options...
Slip Posted October 9, 2008 Share Posted October 9, 2008 Okay here is a quick script to test a unique array; $array = array("apple","pear","banana","apple"); // create a string out of the array for($i=0; $i<count($array); $i++) { $string .= $array[$i]; } // search the string $testString = "pear"; if(substr_count($string, $testString) == 1) { echo "{$testString} is unique!"; } else { echo "{$testString} is not unique"; } it works leaving the original $array untouched. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 9, 2008 Share Posted October 9, 2008 Are these results from a database query? if so, you can skip using the class and just loop through the multi dimensional array like this <?php $arr = array( array('first_name' => 'ab', 'last_name' => 'bb'), array('first_name' => 'ac', 'last_name' => 'bb'), array('first_name' => 'ad', 'last_name' => 'bbl'), array('first_name' => 'ae', 'last_name' => 'bbl'), array('first_name' => 'af', 'last_name' => 'bbt'), array('first_name' => 'ag', 'last_name' => 'bbr'), array('first_name' => 'ah', 'last_name' => 'bbr'), array('first_name' => 'ai', 'last_name' => 'bbr'), array('first_name' => 'aj', 'last_name' => 'bbs'), array('first_name' => 'ak', 'last_name' => 'bbs'), array('first_name' => 'al', 'last_name' => 'bbv') ); $result = array(); $iteration = 0; foreach($arr as $name){ /** * prepare the first name based on teh last name * if the current last name is the same as the previous one append the second letter of the first name to the first name * else, just use the fist letter */ $first_name = $name['first_name']{0}; if($iteration++ != 0 && ($name['last_name'] === $arr[($iteration -2)]['last_name'])){ //-2 because we increment $iteration inline $first_name .= '.'. $name['first_name']{1}; } $result[] = $first_name .' '. $name['last_name']; } echo '<pre>', print_r($result), '</pre>'; ?> this is assuming that the last names are already in order and your array is a multidimensional array which is easy to do when querying from a database Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted October 9, 2008 Author Share Posted October 9, 2008 I'm using PHP 5 if that makes a difference. MY PHP 4 signature is a joke. Obviously not a good one. My data isn't from a database. It comes in various ways, so I can create it into an array however needed. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 9, 2008 Share Posted October 9, 2008 Look at the code in my last example. Set up your array like it is there array( array('first_name' => 'joe', 'last_name' => 'smith') ); then run it through the class here (http://www.phpfreaks.com/forums/index.php/topic,220273.0.html) there are usage examples then run sorted output through the code that i wrote above and you'll have you answer Quote Link to comment 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.