joequah1 Posted September 22, 2011 Share Posted September 22, 2011 hi, Im getting data from a database and store in an array. and there will be a lot of repeating data in the array. I would like to know if there is any function can filter the array and arrange in a nice position. or there's no way doing it and need to check it manually? appreciate for the help. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/247637-filter-same-data-in-an-array/ Share on other sites More sharing options...
JonnoTheDev Posted September 22, 2011 Share Posted September 22, 2011 If you want to remove duplicates in an array simply use array_unique() <?php $x = array('red','green','blue','red'); $x = array_unique($x); print_r($x); ?> Alternatively when you are building and array do not allow duplicates to be inserted into it. You can us the functions in_array() or array_key_exists() to check for values already in the array. <?php $x = array('red','green','blue','red','green','blue','red','green'); $y = array(); foreach($x as $colour) { if(!in_array($colour, $y)) { $y[] = $colour; } } print_r($y); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247637-filter-same-data-in-an-array/#findComment-1271670 Share on other sites More sharing options...
joequah1 Posted September 22, 2011 Author Share Posted September 22, 2011 If you want to remove duplicates in an array simply use array_unique() <?php $x = array('red','green','blue','red'); $x = array_unique($x); print_r($x); ?> Alternatively when you are building and array do not allow duplicates to be inserted into it. You can us the functions in_array() or array_key_exists() to check for values already in the array. <?php $x = array('red','green','blue','red','green','blue','red','green'); $y = array(); foreach($x as $colour) { if(!in_array($colour, $y)) { $y[] = $colour; } } print_r($y); ?> the second way can apply in reading from database right? as for the first way, can make the arrangement in the correct way? eg. if i put red in the third place and blue in the fourth place, when you print out it will skip index 3. thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/247637-filter-same-data-in-an-array/#findComment-1271671 Share on other sites More sharing options...
JonnoTheDev Posted September 22, 2011 Share Posted September 22, 2011 the second way can apply in reading from database right? Yes, if you are constructing an array from a database query i.e <?php $result = mysql_query("SELECT name FROM users WHERE job='Web Developer'"); $developers = array(); while($row = mysql_fetch_assoc($result)) { if(!in_array($row['name'],$developers)) { $developers[] = $row['name']; } } ?> as for the first way, can make the arrangement in the correct way? eg. if i put red in the third place and blue in the fourth place, when you print out it will skip index 3. The array_unique() function removes duplicates from the array as I have stated, therefore the array will only contain red, green, blue in that order (indexes 0,1,2). If you want to sort an array you can use a variety of functions. Why not check over the php manual and read the examples: http://uk.php.net/sort There are loads of functions for sorting arrays in the left column. Also, data should really be sorted from your initial database query using ORDER BY, etc. Also I could only query unique records from my database so I do not have to have any filter logic in my application code i.e <?php $result = mysql_query("SELECT DISTINCT name FROM users WHERE job='Web Developer' ORDER BY name ASC"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247637-filter-same-data-in-an-array/#findComment-1271674 Share on other sites More sharing options...
joequah1 Posted September 22, 2011 Author Share Posted September 22, 2011 the second way can apply in reading from database right? Yes, if you are constructing an array from a database query i.e <?php $result = mysql_query("SELECT name FROM users WHERE job='Web Developer'"); $developers = array(); while($row = mysql_fetch_assoc($result)) { if(!in_array($row['name'],$developers)) { $developers[] = $row['name']; } } ?> as for the first way, can make the arrangement in the correct way? eg. if i put red in the third place and blue in the fourth place, when you print out it will skip index 3. The array_unique() function removes duplicates from the array as I have stated, therefore the array will only contain red, green, blue in that order (indexes 0,1,2). If you want to sort an array you can use a variety of functions. Why not check over the php manual and read the examples: http://uk.php.net/sort There are loads of functions for sorting arrays in the left column. Also, data should really be sorted from your initial database query using ORDER BY, etc. Also I could only query unique records from my database so I do not have to have any filter logic in my application code i.e <?php $result = mysql_query("SELECT DISTINCT name FROM users WHERE job='Web Developer' ORDER BY name ASC"); ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/247637-filter-same-data-in-an-array/#findComment-1271675 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.