PHP - Php - How To Compare And Eliminate Repeated Data Set Of Arrays?
Hi,
please help me to solve my query... I was incurring a problem in executing a PHP code which executes 'n' number of data sets..
For example -- in the below shown outputs, I need to eliminate executing the 4th output which is similar (repeated data) to the 1st output,
input output
4,4,5,6,5 4,4,5,5,6
4,4,5,7,5 4,4,5,5,7
4,4,5,7,6 4,4,5,6,7
4,4,6,5,5 4,4,5,5,6
Kindly send me the function details to execute it without repetition.
Thanks a lot for your support!
Regards,
Maanu
Similar TutorialsI need to compare several arrays...to narrow down a bunch of mysql selects. I can't figure out which of the array_diff, array_unique, array_merge options there are. I need to get an array of umpires (baseball), then compare that to the array of umpires that can work a given level, then compare that to the array of umpires that can work at that school, and finally compare that to the array of umpires who have asked for the day off. In the code below, all of the selects work...the echo's that are commented out printed out what I expected them to. I'm having a problem with the array comparisons at the end of this code: Code: [Select] <?php //connect to database $dbc = mysql_pconnect($host, $username, $password); mysql_select_db($db,$dbc); //get info of game $sql = "SELECT sport,home,day FROM games WHERE `game_id` = $_GET[game]"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $game_array = $row['sport']; $home_team_array = $row['home']; $game_day_array = $row['day']; } //list of all umpires in association $sql = "SELECT ump_id FROM ump_names WHERE '$_SESSION[association_id]' = `association_id`"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $name_array[] = $row['ump_id']; // echo "<br /><br />All Ump ID's Array = "; // foreach($name_array as $name_array_final) { // echo $name_array_final . "<br />"; // } } //get bad schools list $sql = "SELECT ump_id FROM bad_school WHERE '$_SESSION[association_id]' = `association_id` AND `school` = '$home_team_array' and `sport` = '$game_array'"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $bad_school_array[] = $row['ump_id']; // echo "<br /><br />Bad School Array = "; // foreach($bad_school_array as $bad_school_array_final) { // echo $bad_school_array_final . ", "; // } } //get official list at this game's level $sql = "SELECT ump_id FROM ump_names WHERE `association_id` = '$_SESSION[association_id]' AND `$game_array` = '1'"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $official_level_array[] = $row['ump_id']; // echo "<br /><br />Officials that can work this level of game = "; // foreach($official_level_array as $official_level_array_final) { // echo $official_level_array_final . ", "; // } } //get vacation list $sql = "SELECT ump_id FROM days_off WHERE '$_SESSION[association_id]' = `association_id` AND `day` = '$game_day_array'"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $days_off_array[] = $row['ump_id']; // echo "<br /><br />Officials that are on vacation this day = "; // foreach($days_off_array as $days_off_array_final) { // echo $days_off_array_final . ", "; // } } //$first_step = array_merge($official_level_array, $name_array); $first_step = array_merge($official_level_array, $name_array); $first_step_part_2=array_unique($first_step); //$second_step = array_merge($bad_school_array, $first_step); $second_step = array_merge($bad_school_array, $first_step_part_2); $second_step_part_2=array_unique($second_step); //$third_step = array_merge($days_off_array, $second_step); $third_step = array_merge($days_off_array, $second_step_part_2); $third_step_part_2=array_unique($third_step); echo "These umpires are available for this game:<br />"; foreach ($third_step as $iamavailable) { echo "$iamavailable<br />"; } ?> This prints out: Quote 2 1 2 4 5 6 10 14 15 3 7 8 9 11 12 13 16 17 It should only print out 4, 5, 6, 10, 14, 15. What I want to do is: compare $official_level_array and $name_array...only keep those that are in $official_level_array. Then, compare that result with $bad_school_array...removing those in the $bad_school_array...and keeping the ones that are left. Then, compare that result, with $days_off_array...removing those in the $days_off_array...and keeping the ones that are left. Print out what's left... Where am I going wrong? Hi guys, I am new to PHP/coding and am trying to look for 1. A way of comparing the words in one static array against other dynamically created arrays (created from mysql queries) 2. Work out how many similar words there are - then assign that number to that array My static array is...$comparewithme = array with values = this is an example of an array Mysql_query("select id, words from table_example") Results from query are put into an array that is named according to id.. $result2 = array with values = this is an example of queries $result3 = array with values = this is not an example of php Comparison should give the following info Comparing $comparewithme with $result2 should generate a hit rate of 5 (similar words=this is an example of)... Comparing $comparewithme with $result3 should generate a hit rate of 4 (similar words=this is an example)... Any ideas greatly appreciated...thanks in advance I have two arrays, X and Y, which contain names. The arrays are of the same size. The arrays are related to each other such that for each pair X[n] , Y[n] X is friends with Y. For example: X[0,1,2] = [Link, Cloud, Cloud, Mario, Mario, Luigi] Y[0,1,2] = [Zelda, Barrett, Tifa, Luigi, Bowser, Mario] Link is friends with Zelda Cloud is friends with Barrett and Tifa Mario is friends with Luigi and Bowser Luigi is friends with Mario I want to loop through these arrays and, for each unique name, find all of that person's friends. I then want to print the results to a text file, like so: Link, Zelda Cloud, Barrett, Tifa Mario, Luigi Luigi, Mario I know how to do this theoretically, but I just need help with the PHP syntax. Thanks very much. Hi, I have a database (mysql). In the database is a table with categories (categories_id, categories_name etc). I also have another table (products table) which gets filled using a html form. That table holds category info too. What I am trying to do (without any luck) is when the form is processed, it will populate the table with the new data, then I need it to compare the 2 tables to see if the category fields from both tables have a match. If there is a match in the tables then send an email to people who has asked to be notified when new product has been added that matches a particular criteria. For example, if someone adds a product to the category 'Landscapers', with a location 'Dorset'... I would like to be able to email all the registered tradespeople in Dorset about the new product. I know this should be something quite simple, but I cant seem to get my head around it. Any pointers would be fantastic. Many thanks DB Hey Guys, I'm struggling with some logic of how to do something wondering if anyone can point me in the right direction. Details: I am making a PHP/MySQL game to learn more PHP, I've got my login/logout system created. However I am planning on the game been map based on a 100x100 grid. I have two tables: USERS Username, Password, x, y Test, Test, 1, 1 MAP X, Y, type 0,1,1 0,2,1 0,3,2 0,4,3 0,5,2 etc I am stuck on how to get the user's logged in X and Y coordinates and then compare them with the map table X and Y, to see what type of field to show, 1,2 or 3. Is there a name for this type of function so I can do some research on it? Or could someone give me an example? Would be fantastic if anyone has any advice. Thanks in advance NewcastleFan Hi, I am trying to split a string into an array and then seeing what sort of data it is. eg; I get the following string Quote api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse I need to be able to put this into an array so in this case Code: [Select] myarray[type]=somethingelse myarray[api]=somerandomkeyhere myarray[user]=someuser myarray[password]=somehashvaluehere but it could include many more/less different value's separated by the & symbol. Thanks, -mme Hello , I have a table like $data[$var][$i] $data is an string, $var is a string and $i is an index int. I am tired to write for($i=1;,$i<=count($$data[$var],$i++) { echo $data['id'][$i] ... }oouuuff How i can make it with foreach ? I tried foreach ($data -> $field as $value) echo $value; but i doesnt work. Any advise ? i have a text file that has spaces between each column. eg 12 12 13 22 34.5 10 13 18 88 32.5 12 33 17 23 22.3 (the actual data is weather data that accumulates a new line every 15 mins and is seen at www.maidenerleghweather.com/clientraw.txt ) each column represents a weather variable. i would like to use php to find the maximum values in each column and minimum values etc etc. how can i get the text data into arrays, so that i can then easily perform some simple stats on it? HOWEVER each line would NOT be an array. the first value in each line makes up array number one, second makes up two etc any ideas for a newbie beginner would be welcomed. Hi there, I am wanting to insert post data into my DB, this is guest information if i do a $safePost = filter_input_array(INPUT_POST); print_r($safePost); i get the following output. Array ( [action] => addGuestInfo [guestno] => 2 [bookingid] => 151 [customerid] => 22 [Guestname-0] => Jamie [Guestname-1] => Joe [GuestAge-0] => 4-3 [GuestInfo-0] => celiac [GuestAge-1] => 18 [GuestInfo-1] => wheat ) normally the guestname-0, guestage-0, guestinfo-0 would be in correct order but we have to ensure the just incase it isns't (see output above) we cater for that aswell, we can have upto 6 guest details coming in at once. so I need to loop through the data and insert into my DB. I have tried various for / foreach loops but cant seem to get it to group the data together I have tried things like below, but it inserts each record multiple times. $safePost = filter_input_array(INPUT_POST); foreach($safePost as $key => $value){ $exp_key = explode('-', $key); // for ($i =0; $i <= $guestcount; $i++) { switch ($exp_key[0]) { case 'Guestname': $booking->guestno = $exp_key[1]; $booking->guestname = $val; break; case 'GuestAge': $booking->guestno = $exp_key[1]; $booking->guestage = $val; break; case 'GuestInfo': $booking->guestno = $exp_key[1]; $booking->guestinfo = $val; break; default: break; } //CALLS FUNCTION TO INSERT TO DB result = lastInsertId(); $result= $booking->insertGuestDetails(); //} }
Hi There all, having an issue with simple xml... hopefully someone will be able to shed some light on this. I have this data in an xml feed from an API: <ID>job no 1 </ID> <Name>job name</Name> <State>job status</State> <Tasks> <Task> <Name>task name 1</Name> </Task> <Task> <Name>task name 2</Name> </Task> <ID>job no 2</ID> <Name>job name</Name> <State>job status</State> <Tasks> <Task> <Name>task name 1</Name> </Task> <Task> <Name>task name 2</Name> </Task> I would like to display these in a table pretty much as it is shown above. Job no 1 job name job status job 1 task name 1 job 1 task name 2 Job no 2 job name job status job 2 task name 1 job 2 task name 2 etc my code looks like this : $required is narrowing down specific states not shown for clarity $xml_current=simplexml_load_string($jobs_task_response) or die("Error: Cannot create object"); foreach($xml_current->Jobs->Job as $item_current) { if (in_array((string)$item_current->State, $required)) { $projects_current[] = array( 'job_no' => (string)$item_current->ID, 'job_name' => (string)$item_current->Name, 'job_status' => (string)$item_current->State, ); foreach($item_current->Tasks->Task as $current_tasks){ $projects_task[] = array( 'job_tasks' => (string)$current_tasks->Name, ); } } } foreach ($projects_current as $proj_current) { $job_no =$proj_current['job_no']; $job_name =$proj_current['job_name']; $job_status =$proj_current['job_status']; $clr_current = $colors[$job_status]; $project_id = $job_no; $tdata_1 .= "<tr id='current' class='card-body collapse-show'>"; $tdata_1 .= "<td class='th-sm-1 bg-white '><a href='../details/index.php?pid=$job_no' class='text-left ml-1'>$job_no</a></td>"; $tdata_1 .= "<td data-target='#" . $job_no . "' data-toggle='collapse' class='th-sm-2 bg-white text-left ml-1'>$job_name</td>"; $tdata_1 .= "<td class='th-sm-2 " . $clr_current . " text-left ml-1 '>$job_status</td>"; foreach ($projects_task as $proj_tasks){ $job_tasks =$proj_tasks['job_tasks']; $tdata_1 .= "</tr>"; $tdata_1 .= "<tr id='" . $job_no . "' class='card-body collapse'> "; $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>"; $tdata_1 .= "<td class='th-sm bg-white text-left ml-1'></td>"; $tdata_1 .= "<td class='th-sm " . $clr_current . " text-left ml-1'>$job_tasks</td>"; $tdata_1 .= "</tr>"; } } what I get with this code however is; Job no 1 job name job status job 1 task name 1 job 1 task name 2 job 2 task name 1 job 2 task name 2
Job no 2 job name job status job 1 task name 1 job 1 task name 2 job 2 task name 1 job 2 task name 2
I'm sure there is a simple answer to this, I just can't seem to put my finger on it. Can anyone help please? Much appreciated thanks in advance! Ok so I'm trying to use jpgraph which allows you to make graphs with php (you dont need to know anything about jpgraph to help me) anyway how it works is this line data = array(1,3,4,5); the numbers here are what the graph plots. now i'm trying to figure out a way to place values from my database in there. the problem is the amount of values is constantly changing. I tried a lot of things but nothing is working here is my last try: $result = mysql_query("SELECT thismonth FROM list"); while ($row = mysql_fetch_array($result)) { $data[] = $row[0]; } $number = count($data); for ($i = 0; $i < $number; $i++) { $wtf .= $data[$i] . ',' ; } now when i echo $wtf i get values such as 6,3,2,0,1 now i tried this $data = array($wtf); but it doesnt work any ideas? Hi wondering if there's a simple way to merge or update the data from one array to another . I've tried merge data but it just adds additional records rather than merging them off one common key.
The array is just simply adding a name to another array the format of both is as follows
Array 1
[0] => stdClass Object ( [name] => [market] => Football [selection] => 7051575 ) ) Array 2 Array ( [0] => stdClass Object ( [name] => Something [market] => [selection] => 7051575 ) ) There are other fields but I'm just trying to merge the name from Array two into Array one using the selection as a common key. Is there an easy command like merge or do I have to loop thru the arrays to add the data. Thanks how do you use a while loop 2 twice from one assoc array? eg <tr> <td>1st favourite</td> <td><select name = 'fav_1'>"; while($Cat = mysql_fetch_assoc($rCat)) { echo " <option value='{$Cat['catID']}'>{$Cat['categorys']}</option>";} echo "</td> </tr> <tr> <td>2nd favourite</td> <td><select name = 'fav_2'>"; while($Cat = mysql_fetch_assoc($rCat)) { echo " <option value='{$Cat['catID']}'>{$Cat['categorys']}</option>";} echo "</td> </tr> The first one works fine but the second is empty. <?php require_once('upper.php'); require_once('database.php'); require_once('LoginStatement.php'); $LoginId=$_COOKIE['LoginIdCookie']; $query="select * from activity_participation where LoginId='$LoginId'"; $result=mysqli_query($dbc,$query) or die ('Not Connected'); //echo "<div class='search_output_data'> echo "<table border='0' cellspacing='0' cellpadding='0'>"; while($row=mysqli_fetch_array($result)) { echo "<tr><th align='left' valign='top'>Activity Title</th> <th align='left' valign='top'>Date of Participation</th> </tr> <tr><td colspan='4' align='left' valign='top'><table align='left' border='0' cellspacing='0' cellpadding='0'><tr><td align='left' valign='top'>".$row['Title']."</td> <td align='left' valign='top'>".$row['Date']."</td> </tr></table> </td></tr>"; } echo "</table>"; //echo "</div>"; require_once('lower.php'); ?>In table activity_participation many rows have same values. I want to display only different rows to user not to display same rows again and again. Or Anybody have idea to prevent the row duplication in database.... Help me please......... thanks in advance............. Hey all, first post to this site. I'm somewhat new to PHP, so bear with me - this might be an easy question, it might not be. Just looking for a little help. Basically, I have a query which takes commands from a form through AJAX (day, time, time1). The query executes a wildcard on a table named sip_data, searches for linked $id (so if $id=3, for example in multiple tables, it spits out the $name, $zip in a div). Here's the code: Code: [Select] <?php $dbhost = "localhost"; $dbuser = ""; $dbpass = ""; $dbname = ""; //Connect to MySQL Server $link = mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); // Retrieve data from Query String $id = $_GET['id']; $name = $_GET['name']; $zip = $_GET['zip']; $server_url = $_GET['server_url']; $day = $_GET['day']; $time = $_GET['time']; $time1 = $_GET['time1']; // Escape User Input to help prevent SQL Injection $id = mysql_real_escape_string($id); $name = mysql_real_escape_string($name); $zip = mysql_real_escape_string($zip); $server_url = mysql_real_escape_string($server_url); $day = mysql_real_escape_string($day); $time = mysql_real_escape_string($time); $time1 = mysql_real_escape_string($time1); //build query $query = "SELECT * FROM $day,sip_data WHERE $day.id=sip_data.id AND $day$time<=>$day$time1 ORDER BY zip ASC"; //Execute query $qry_result = mysql_query($query) or die(('No Results')); //Build Result String while($row = mysql_fetch_array($qry_result)) { echo "<table id=query_result align=left>"; echo "<tr>"; if($zip = $row[zip]); echo "<th><b>$row[zip]</b></th>"; echo "</tr>"; echo "<tr>"; echo "<td><a href=$row[server_url] rel=ajaxDiv>$row[name]</a></td>"; echo "</tr>"; echo "</table>"; } echo $display_string; ?> Basically, my issue with this is that some of the results within the sip_data DB will have zip codes that repeat. The current code prints out each results with the zip code and name - here's an image of a sample result: What I would like to do is avoid repeating the zip code and just group the results under each zip code, kinda like this: 55408 55412 55423 --------- ------------ ---------- example example example example example example Any help would be greatly appreciated. I feel like I'm so close to the answer, but just need a little guidance. Who knows, I might be way off. Thanks in advance! So I am trying to find a good alternative to this code: Code: [Select] function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } I did a few searches here and just keep coming up with instances of its use rather than a solution as I was hoping for. I was also checking the php manual and found user comments on it, but am not sure what is best to use rather than the magic_quotes. Any suggestions? Hello All,
I've encountered one more problem (with the first WebApp I'm building), and I have no idea how to fix it. I've tried something, but it doesn't seem to work, so I'd appreciate some help in getting it fixed.
Here's what's happening:
So after I perform an operation (let's say "ADDing a record")...the records gets added correctly, but then, if/when I press [F5] in my browser window (or click on the Refresh/Reload icon), the same input details are being used and a second identical record is being added. I believe the same thing happens for some (or perhaps all) of the other CRUD operations.
What do I need to do to disable that behavior?
The only thing I could think of is to set the $functionSelected variable to blank ("") - which I've done towards the bottom of my code (shown below) - but that doesn't seem to be doing anything...the problem persists.
if(isset($_POST['action'])) { $functionSelected = $_POST["action"]; } if(isset($functionSelected)){ switch($functionSelected){ case "insert": $initialize_flag = "N"; // Store POST(ed) user-entered values into memory variables $store_name = $_POST['store_name']; $item_description = $_POST['item_description']; $qty_pkg = $_POST['qty_pkg']; $pkg_of = $_POST['pkg_of']; $price = $_POST['price']; $flyer_page = $_POST['flyer_page']; $limited_time_sale = $_POST['limited_time_sale']; $flyer_date_start = $_POST['flyer_date_start']; $nos_to_purchase = $_POST['nos_to_purchase']; // Setup customized query $sql = "INSERT INTO shoplist (store_name, item_description, qty_pkg, pkg_of, price, flyer_page, limited_time_sale, flyer_date_start, nos_to_purchase, shopper1_buy_flag, shopper2_buy_flag, purchased_flag, purchase_later_flag) VALUES (:store_name, :item_description, :qty_pkg, :pkg_of, :price, :flyer_page, :limited_time_sale, :flyer_date_start, :nos_to_purchase, :shopper1_buy_flag, :shopper2_buy_flag, :purchased_flag, :purchase_later_flag)"; try { $statement = $conn->prepare($sql); $statement->bindParam(':store_name', $store_name); $statement->bindParam(':item_description', $item_description); $statement->bindParam(':qty_pkg', $qty_pkg); $statement->bindParam(':pkg_of', $pkg_of); $statement->bindParam(':price', $price); $statement->bindParam(':flyer_page', $flyer_page); $statement->bindParam(':limited_time_sale', $limited_time_sale); $statement->bindParam(':flyer_date_start', $flyer_date_start); $statement->bindParam(':nos_to_purchase', $nos_to_purchase); $statement->bindParam(':shopper1_buy_flag', $initialize_flag); $statement->bindParam(':shopper2_buy_flag', $initialize_flag); $statement->bindParam(':purchased_flag', $initialize_flag); $statement->bindParam(':purchase_later_flag', $initialize_flag); // Execute the query $statement->execute(); $statement->closeCursor(); $functionSelected = ""; // WILL THIS STATEMENT NOT SERVE TO INITIALIZE/RESET THE OPERATION??? break; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; } case "update-delete": ... ... ...Thanks. Hi - To me, this doesn't make sense. My Order form uploads product weights in an array . If I echo out those weights they are fine ie 25.25 However, when I try to update the DB for some reason, the for loop strips out all but the first digit, so 25.25 becomes '2' or indeed if I had entered 9999999 it just '9' is entered in to the DB. I have tried using number_format to pre-process the decimals, - but I get an error saying you can not use it in a write format. Many Thanks to anyone who can help with this !! PS - I have abbreviated the below code for clarity: Code: [Select] $weight = $_POST['weight']; for ($i = 0; $i < $numloops ; $i++) { $sql =" UPDATE `confirmedorder` SET orderid = '$orderid[$i]', weight='$weight[$i]',status='finished', ordervalue='$ordervalue' WHERE customerid = $customerid "; $this->db->query($sql); } This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=345046.0 |