array repetition help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imananimeaddict
Forum Newbie
Posts: 9
Joined: Wed Mar 03, 2010 2:06 pm

array repetition help!

Post by imananimeaddict »

hello,
i have an array that contains building data, i wish to remove all of the repeated elements so that i have a new array with no repeats.

Code: Select all

 
Array ( 
          [0] => Array ( [building_id] => 54 [building_name] => Sholden Retirement Home ) 
          [1] => Array ( [building_id] => 54 [building_name] => Sholden Retirement Home ) 
          [2] => Array ( [building_id] => 60 [building_name] => u ) 
          [3] => Array ( [building_id] => 60 [building_name] => u ) 
)
-->
Array ( 
          [0] => Array ( [building_id] => 54 [building_name] => Sholden Retirement Home ) 
          [1] => Array ( [building_id] => 60 [building_name] => u ) 
)
 

i have got it to work with just building_id using array_count_values, then iterating round the arrays until i get a match. it but it wont seem to work when i add a second element to the array :(

can anyone help?
thank you.
Last edited by imananimeaddict on Tue Mar 09, 2010 9:29 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: array repetition

Post by requinix »

Where did the array come from?
imananimeaddict
Forum Newbie
Posts: 9
Joined: Wed Mar 03, 2010 2:06 pm

Re: array repetition

Post by imananimeaddict »

in our database we have a buildings table, and an events table, and a schedule table that links it all together.
if an event is scheduled to occur then a new row is added to the schedule table.

when i bring back all active events filtered around my parameters like date and user
i get a list similar to above with loads more attributes but didn't think it was necessary to include them all, which it fab for the table data.

however in the buildings table we also bring back lng and lat. for our google maps. our google map pins have a semi transparent shadow. so when there is more than one building of the same lng lat the shadow gets darker. which is very frustrating.

so we would like to be abel to remove duplicate building from the array then the shadows will all be the same.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: array repetition help!

Post by requinix »

Modify your SQL to only return unique rows.
imananimeaddict
Forum Newbie
Posts: 9
Joined: Wed Mar 03, 2010 2:06 pm

Re: array repetition help!

Post by imananimeaddict »

how do i do that??

this is my query, i added distinct at the top, but doesn't seem to change anything.
as event information is returned too so the rows aren't the same. as an event may occur at different times or dates but still be in the same building causing the double shadow

Code: Select all

 
 
$q="SELECT DISTINCT schedule.schedule_id, schedule.date, schedule.start_time, schedule.end_time,buildings.lon,buildings.lat,buildings.name,buildings.building_id,events.event_id,events.organisation_id, events.name AS event_name, buildings.name AS building_name FROM buildings INNER JOIN (events INNER JOIN schedule ON events.event_id = schedule.event_id) ON buildings.building_id = schedule.building_id WHERE ((events.user_id='$user_id')OR (events.organisation_id IN('$orglist3')) )AND schedule.date>=curdate()";
 
$r= mysqli_query($dbc, $q); // run the query
$num_rows = mysqli_num_rows($r);
while($row = mysqli_fetch_array($r))
    {
    $building_id[] = $row["building_id"];
    }
    print_r($building_id);
 
 
 
i only want one of each building id to be returned...

Code: Select all

 
Array ( [0] => 54 [1] => 54 [2] => 60 [3] => 60 [4] => 60 [5] => 60 [6] => 60 [7] => 60 [8] => 60 [9] => 60 [10] => 60 [11] => 60 [12] => 54 [13] => 54 [14] => 39 [15] => 39 [16] => 39 [17] => 39 [18] => 39 [19] => 39 [20] => 39 [21] => 39 [22] => 45 [23] => 25 
 
User avatar
garygay
Forum Newbie
Posts: 5
Joined: Thu Mar 04, 2010 4:03 am

Re: array repetition help!

Post by garygay »

array_unique

take a look :mrgreen:
imananimeaddict
Forum Newbie
Posts: 9
Joined: Wed Mar 03, 2010 2:06 pm

Re: array repetition help!

Post by imananimeaddict »

will that work on a muli-dimensional array?

Code: Select all

 
 
$q="SELECT DISTINCT schedule.schedule_id, schedule.date, schedule.start_time, schedule.end_time,buildings.lon,buildings.lat,buildings.name,buildings.building_id,events.event_id,events.organisation_id, events.name AS event_name, buildings.name AS building_name FROM buildings INNER JOIN (events INNER JOIN schedule ON events.event_id = schedule.event_id) ON buildings.building_id = schedule.building_id WHERE ((events.user_id='$user_id')OR (events.organisation_id IN('$orglist3')) )AND schedule.date>=curdate()";
 
$r= mysqli_query($dbc, $q); // run the query
$num_rows = mysqli_num_rows($r);
while($row = mysqli_fetch_array($r))
    {
            $thelist[$i]['building_id'] = $building_id;
            $thelist[$i]['building_name'] = $building_name;
            $thelist[$i]['building_lat'] = $building_lat;
            $thelist[$i]['building_lon'] = $building_lon;
            $i++; 
    }
    $result = array_unique($thelist);
        print_r($result);
 
 
this just gives me an empty array...

all 4 of those will be exactly the same if a building is repeated, so the array isn't unique...
w1n78
Forum Newbie
Posts: 12
Joined: Mon Mar 08, 2010 10:55 pm

Re: array repetition help!

Post by w1n78 »

according to http://php.net/manual/en/function.array-unique.php
Note: Note that array_unique() is not intended to work on multi dimensional arrays.
i agree with tasairis and change your query so that you get unique values. you can use group by and remove unnecessary columns from your select part of the query. having the extra columns in your select could still include duplicates when using the group by
imananimeaddict
Forum Newbie
Posts: 9
Joined: Wed Mar 03, 2010 2:06 pm

Re: array repetition help!

Post by imananimeaddict »

i have added the group by which seems to work wonders!
it is a tad odd tho, it seems to return all the queries twice, even the once that are only in the data once...
i added a count and splice to remove the extra data.. assuming it will always do this and want cause an error else where.

any ideas why i get a repetition?

Code: Select all

 
$q="SELECT DISTINCT schedule.schedule_id, schedule.date, schedule.start_time, schedule.end_time,buildings.lon,buildings.lat,buildings.name,buildings.building_id,events.event_id,events.organisation_id, events.name AS event_name, buildings.name AS building_name FROM buildings INNER JOIN (events INNER JOIN schedule ON events.event_id = schedule.event_id) ON buildings.building_id = schedule.building_id WHERE ((events.user_id='$user_id')OR (events.organisation_id IN('$orglist3')) )AND schedule.date>=curdate() GROUP BY buildings.building_id";          
 
$r= mysqli_query($dbc, $q); // run the query
$num_rows = mysqli_num_rows($r);
while($row = mysqli_fetch_array($r))
    {
            $thelist[$i]['building_id'] = $row["building_id"];
            $thelist[$i]['building_name'] = $row["building_name"];
            $thelist[$i]['building_lat'] = $row["lat"];
            $thelist[$i]['building_lon'] = $row["lon"];
            $i++; 
    }
    $countit=count($thelist);
    //half the array
    $countit = $countit/2;
    array_splice($thelist, $countit);
    sort($thelist);
    print_r($thelist);
// i hope it ALWAYS returns all the buildings twice. it works for yr login and mine.    
 
Testing was only returned once by the query before..

Code: Select all

 
[color=#8040BF]Array ( [] => Array ( [building_id] => 72 [building_name] => ownership [building_lat] => 51.2192561 [building_lon] => 1.4000388 ) [1] => Array ( [building_id] => 46 [building_name] => Testing Two [building_lat] => 51.2233922 [building_lon] => 1.3856348 ) [2] => Array ( [building_id] => 45 [building_name] => Testing [building_lat] => 51.2231481 [building_lon] => 1.4022725 [/color])[color=#804000] [3] => Array ( [building_id] => 45 [building_name] => Testing [building_lat] => 51.2231481 [building_lon] => 1.4022725 ) [4] => Array ( [building_id] => 46 [building_name] => Testing Two [building_lat] => 51.2233922 [building_lon] => 1.3856348 ) [5] => Array ( [building_id] => 72 [building_name] => ownership [building_lat] => 51.2192561 [building_lon] => 1.4000388 ) ) [/color]
 
Post Reply