FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


take out repeat data in array





Mgccl
an array defined like this, how do we take out the repeating data here
so, the array $data we defined as

Code:
$player0['id'] = 0;
$player0['name'] = 'player0';

$player1['id'] = 1;
$player1['name'] = 'player1';
//look, there are one repeated data, 2 player 0's
$player2['id'] = 0;
$player2['name'] = 'player0';

$data[$i]['team_id']= 0;
$data[$i]['team'][] = $player0;
$data[$i]['team'][] = $player1;
$data[$i]['team'][] = $player2;

//so now, $data[$i]['team'][0] and $data[$i]['team'][2] contains the same thing....



but now, I used Code:
Code:
$data[$i]['team'] = array_unique($data[$i]['team']);




turns out that
$data[$i]['team'] will only contain ONE array, which is
Code:
$data[$i]['team'][0]['id']= 0;
$data[$i]['team'][0]['name']= player0;




and all other arrays are gone...which is not what I wanted...
so what if, I only want to take out the 3rd one because the 3rd one have abosutlutely the same data as the 1st? what code shold I write then?
SlowWalkere
If you want to eliminate duplicate data, when you copy the info into the array you should run some kind of check on it to see if it already exists in the array.

There are a couple ways you could do it. I think the simplest would be to use in_array to check if the data already exists in the array.
Code:
if (!(in_array($player1, $data[$i]['team'])))
    $data[$i]['team'][] = $player1;

That should tell you whether or not $player1 already exists in the array, and only add him if he doesn't

Or, you could do it yourself and write a loop that goes through each element of the array to check if $player1 has already been inputted. If he is, a flag is set. At the end, if that flag is unset, add the player, else do nothing.
Code:

$heExists = false;
foreach ($data[$i]['team'] as $player)
{
    if ($player == $player1)
        $heExists = true;
}

if (!$heExists)
    $data[$i]['team'[] = $player1;

With that, you can change the "$player == $player1" line and check for more specific things than just whether or not he's in the array already.

- Walkere
Related topics
script backup database
An Example to mySQL class
Splitting a partition
php and am lost...
How to create a php files that contain a image ?
How to make a website do 'Save as'
Help with Dreamweaver sessions !!!!!!!!
checking password
PHP Mysql admin area? please help
Using Javascript to Encrypt Data then POST to PHP
mysql querries and subquerries problem..
What is AJAX?
Data Mining From tables and then insert into MySQL
Outside phpBB Integration Functions
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.