Jump to content

filter same data in an array?


joequah1

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/247637-filter-same-data-in-an-array/
Share on other sites

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);
?>

 

 

 

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  :D

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");
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.