PHP - Comparing Values Within Two Different Arrays And Pulling My Teeth Out
This has kept me busy for a couple of hours. I have two arrays that I would like to compare against each other to see matches and otherwise. Simple enough, right?
foreach ($array1 as $value){ if (in_array($value, $array2)) { echo $value."-FOUND<br />"; } else { echo $value."-NOT!<br />"; } } Using print_r I can CLEARLY see matches: Code: [Select] array1 = Array ( [0] => CHARLES [1] => TOM [2] => DICK [3] => HARRY) array2 = Array ( [0] => HARRY [1] => DICK [2] => TOM [3] => CHARLES) The only difference I can see is the keys - but why should that matter for what I'm trying to do? Both arrays were converted to uppercase. I have done variations of search_array, array_intersect and array_unique - but absolutely nothing is working. 3 hours later and I'm no further then I started off. WTF? Similar TutorialsHi guys,
I wonder if you can offer any advice?
I have two user database tables that are about 70% identical. The problem is that with one of them, the phone number for each user has been cut short by two digits (it was an error on my part early on in the database design).
What I need to do is to get the values from both tables and put them into separate arrays then compare the arrays in a loop. If, say, the first five digits of the phone number in each row of the first table match one entry in the second table then update the second table with the phone number (the full length one).
I'm about puzzled about how to approach this, I've still got a lot to learn about array functions, it seems like it should be fairly simple but I'm a bit lost as to where to begin.
Any advice would be massively appreciated!
Hello
I have two arrays of mysql values $a1, $b1
I am trying to loop through each $a value, no problem,
but I want to see if each element in $a = one of the values in $b
so:
$a = array(1, 2, 3, 4, 5, 6); $b = array(4, 6); $y = ''; foreach ($a1 as $a){ foreach ($b1 as $b){ if ($a[val] == $b[val]){ $y = '*'; } } echo $y.$a.'<br />'; }I was kinna hoping it would give me 1 2 3 *4 5 *6 Its most definitely not though... Im sure Im flawed in my logic but have coded so much other crap I just can't see the answer! ty for the look/help. Hello I am having a problem getting the difference between two arrays. The first array $list has all codes that can be linked to a specific id The second array $exist has the values select and linked to a specfic id. Both arrays are storing integers array $list should have 110,111,112,113,114,115. Array $exist gets 110, 114 115 from the database the difference should be 111 & 112 I think the problem is with array $exist that is getting its values from the database. Could the problem be that the arrys are storing data differently? How do I resolve this problem? /**==========ARRAY 1=======**/ /**All values for this type in array for comparison against**/ Code: [Select] $list = array('110','111','112','113','114','115'); /**view values in array $list**/ Code: [Select] var_dump($list); array(6) { [0]=> string(3) "110" [1]=> string(3) "111" [2]=> string(3) "112" [3]=> string(3) "113" [4]=> string(3) "114" [5]=> string(3) "115" } /**==========ARRAY 2=======**/ /**Get stored types for specific id **/ Code: [Select] $exist = array();//create array //store values in array $query = "SELECT type FROM contact WHERE id ='$id' AND deleted = 'N' ORDER BY type"; $result = mysqli_query ($mysqli, $query); while($row = mysqli_fetch_array($result)) { $exist[] = $row; } /**View values in array for specific id in array $exist**/ Code: [Select] var_dump($exist); array(3){ [0]=>array(2){ [0]=>string(3) "110" ["contact_type"]=> string(3) "110"} [1]=> array(2){[0]=> string(3) "114" ["contact_type"]=> string(3) "114"} [2]=> array(2){ [0]=> string(3) "115"["contact_type"]=> string(3) "115"}} /**==========RESULT=======**/ /**Get the difference between all possible type and stored values linked to a specific id **/ Code: [Select] $difference = array_diff($list, $exist); /**viewthe difference between the two arrays**/ Code: [Select] var_dump($difference); array(6) { [0]=> string(3) "110" [1]=> string(3) "111" [2]=> string(3) "112" [3]=> string(3) "113" [4]=> string(3) "114" [5]=> string(3) "115" } Hello everyone, I'm currently working on a large inventory management screen; the website has multiple "distributors" with their own inventory of products. Each distributor has the potential to carry the entire catalog of SKU's so right now, I have the stock/sku/dealer ID/product ID in one table with the product data/ID in another and am only inserting new rows if they are carrying the product. However they want to manage all of the inventory at once so I need to determine which IDs are update queries and which are insert queries. I'm trying to achieve this by comparing an array of the submitted data against an array of their existing inventory. If they don't have an entry for the submitted data key, we need to insert a new row for it to track their inventory. I just can't figure out how to get the keys to line up. Here is what I have; I tried to put it all into obviously defined variables for this forum. Code: (php) [Select] // assign our submitted values to an array $submitted_data = $_POST; // remove the SKUs with 0 or no inventory submitted $submitted_data = array_filter($submitted_data); // pull the distributor id out of the array before the insertion loop $distributors_inventory_distributor_ID = array_pop($submitted_data); // pull all existing inventory for this distributor to see if we are adding new inventory // or updating old inventory $existing_Inventory_Result = mysql_query("SELECT distributors_inventory_product_ID FROM distributors_inventory WHERE distributors_inventory_distributor_ID = $distributors_inventory_distributor_ID ORDER BY distributors_inventory_product_ID ASC", $db); // verify there is a result $existing_Inventory_num_results = mysql_num_rows($existing_Inventory_Result); if ($existing_Inventory_num_results > 0){ while($existing_Inventory_row = mysql_fetch_assoc($existing_Inventory_Result)){ // put existing inventory into an array $existing_Inventory[] = $existing_Inventory_row['distributors_inventory_product_ID']; } } $update_Inventory_Array = array_diff($submitted_data, $existing_Inventory); // print the array [DEBUG ONLY] echo '<h1>$update_Inventory_Array:</h1>'; print_r($update_Inventory_Array); echo '<hr/>'; echo '<h1>$submitted_data:</h1>'; print_r($submitted_data); echo '<hr/>'; echo '<h1>$existing_Inventory:</h1>'; print_r($existing_Inventory); That is outputting this: Quote $update_Inventory_Array: Array ( [963] => 5 [979] => 2 [982] => 2 [974] => 1 [32] => 5 ) $submitted_data: Array ( [963] => 5 [979] => 2 [982] => 2 [974] => 1 [32] => 5 ) $existing_Inventory: Array ( => 32 [1] => 963 [2] => 974 [3] => 979 ) I'd like it to be: Quote $update_Inventory_Array: Array ( [963] => 5 [979] => 2 [974] => 1 [32] => 5 ) $submitted_data: Array ( [982] => 2 ) $existing_Inventory: Array ( [32] => 5 [963] => 1 [974] => 1 [979] => 2 ) Can anyone suggest how to do this? I think if I could make the existing_Inventory array line up with my submitted_data array I could sort the rest out but I'm not sure how to get the keys/data to line up properly. Hello (I think this is more of a php question than MySQL - sorry if I have got it wrong.) I need to know if items have been orphaned (because they lack a cat_id that actually exists). I have two MySQL tables that each have fields 'cat_id'. I have a Category table... cat_id / category 1 / fruit 2 / veg 3 / tools I have another product table cat_id / product 1 / apple 1 / Pear 2 / carrot 2 / cabbage 3 / screwdriver 1 / peach 8 / orphan I have entered the data into two arrays... $categories[] $products[] I have tried a number of combinations of foreach loops to try to determine if the products(cat_id) doesn't match the categories(cat_id) but I seem to be going around in circles. Is there perhaps a function that is available to do this? Or maybe just a straight thinking person that can point out my ineptitude? Thank you for any input. Here are the two arrays to compare. Peopleskills PeopleID SkillID 2 2 2 7 2 9 2 11 3 2 3 12 3 14 4 5 Equipskills EquipID SkillID 1 2 1 9 1 11 2 5 2 7 2 9 2 12 2 13 2 14 3 2 4 11 The common link is SkillID. Each EquipID has some required SkillIDs that people must have to operate that machine. As long as a PeopleID has the required SkillIDs from the EquipSkills, that person has permission to that machine. This will be used create a third array. It will look something like the following. EquipIDs 1 2 3 4 5 6 7 8 9 10 11 12 PeopleIDs 2 x x 3 x 4 5 What might be the best way to code this? Hello fellow coders! My brain is about to explode! Does anyone have some time to help me with a small problem? I have two multi-dimensional arrays similar to the below: Code: [Select] mysql_table_schedule [0] [firstname] = Roger [id] = xxx [1] [firstname] = Pamela [id] = xxy mysql_table_roster [0] [firstname] = Roger [id] = xxx [1] [firstname] = Pamela [id] = xxy [2] [firstname] = Orsen [id] = xyy I can build both arrays above after grabbing the data from the MySQL database by using a quick for function Code: [Select] for($i = 0; $array[$i] = mysql_fetch_assoc($mysql_roster); $i++) ;but after I have both arrays how do I keep only the records in mysql_table_roster that are in the schedule. Out of each daily schedule only a few people work. I'd like to keep all of the associated information from the roster like first name, last name, phone number, etcetera. If yesterday only Roger and Pamela worked how do I compare by [firstname] and trim the mysql_table_roster to inlclude only the arrays related to Roger and Pamela? I've looked into array_intersect, but have no clue how to specify a comparison by a specific array key within multi-dimensional arrays. "Cannot find search daemon". I tried. I've been wracking my brain for the last two hours trying to simplify this. It seems like I'm running over the two arrays far too many times, and that I should be able to do it in one pass somehow. I have two associative arrays, the first contains a set of older options (previous version), the second is a default set of new options. I want to compare the two arrays, and strip out any old options that aren't in the new set. I want to add options from the new set that aren't already in the old, and I don't want to overwrite and of the old options that already have values. Here's a copy of the sandbox I've been working it out in; it works, but it does four separate operations on the arrays to achieve the result: http://pastebin.com/erbFujkG Hi. I have a MySQL database with 4 different fields. id, customer, case and status. No problems of getting them to show up on my page but.. I would like to change the row bgcolor depending of field value on "customer". For example row color would be white until customer changes, then color yellow until customer changes again and back to white etc.. Hope you get the point. Is this possible? Thanks. i've google but no luck on finding what i need....is there a way in javascript to compare 2 variables ie.
var him = "jack"; var her = "jill"; if (him == her){ alert('same name'); }thank you Hi im not sure if this can be done or not but im trying to do a site without using mysql and i want to be able to compare 3 values and depending on the values have them aranged lowest to highest... for example: Apple = 8 Pear = 3 Bannana = 5 so the results would be displayed like... Pear with a total of 3 bannana with a total of 5 Apple with a total of 8 Is this possible using just PHP or will i need to use Mysql as well... Thank you Chris Hi there i have 2 queries one for each table that calculates values in one or more fields. The two tables are different in structure so i have had to calculate them differently: | Ship table | Class |Planet table | Class 1 Class 2 Class 3 Class 4 Depending on what type of ship is added (Class 1-4 ship) its corresponding Class is determined. The planet table is different though as it can have any number of Class 1 - 4 ships. So with help ive put the Ship query results into an array and echoed the results. The Planet query is not in an array as such as i could acheive the same results using the SUM() function. Both work fine and the outputted results look like this: Ship query 1 Class 1 4 Class 2 3 Class 3 1 Class 4 Planet query 1 Class 1 2 Class 2 4 Class 3 13 Class 4 What i really need to do now is compare each queries Class and if the planets class 1-4 < ships class 1-4 then echo the difference. For example the results would be: Class 1 = 0 echo no Class 1 needed Class 2 = - 2 echo no Class 2 needed Class 3 = +1 echo 1 Class 3 needed Class 4 = +12 echo 12 class 4 needed Im sorry if it seems longwinded or if ive not explained my self very well but im finding it difficult. Heres the code: Code: [Select] <?php $colname_resultp = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_resultp = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_resultp = sprintf("SELECT SUM(Class1) As Class1_totla, SUM(Class2) As Class2_totla, SUM(Class3) As Class3_totla, SUM(Class4) As Class4_totla FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text")); $resultp = mysql_query($query_resultp, $swb) or die(mysql_error()); $row_resultp = mysql_fetch_assoc($resultp); $totalRows_resultp = mysql_num_rows($resultp); $colname_Recordset1 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s ORDER BY Class ASC", GetSQLValueString($colname_Recordset1, "text")); $result = mysql_query($query, $swb) or die(mysql_error()); $number_of_results = array(); while($row = mysql_fetch_array($result)){ if (isset($number_of_results[$row['Class']])) { $number_of_results[$row['Class']]++; } else { $number_of_results[$row['Class']] = 1; } } foreach ($number_of_results as $class => $number_of_class) { echo $number_of_class . " Class " . $class; echo "<br />"; } <?php do { ?> <?php echo $row_resultp['Class1_totla']; echo ' Class 1'; echo'<br>'; echo $row_resultp['Class2_totla']; echo ' Class 2'; echo'<br>'; echo $row_resultp['Class3_totla']; echo ' Class 3'; echo'<br>'; echo $row_resultp['Class4_totla']; echo ' Class 4'; ?> <?php } while ($row_resultp = mysql_fetch_assoc($resultp)); mysql_free_result($result); mysql_free_result($resultp); ?> If you could please help that would be ace. Thanks for listening. hey guys, i have my code set up to run substrings through a db to check for matches...even when there is a match it wont insert correctly Code: [Select] $result = mysql_query("select * from friends where userid=$userid"); while($row = mysql_fetch_assoc($result)) { $first_name = $row['first_name']; $last_name = $row['last_name']; $strpos_1 = strpos($friend_1, ' '); $substr_1 = substr($friend_1, 0, $strpos_1); $substr_2 = substr($friend_1, $strpos_1); if($substr_1 == $first_name && $substr_2 == $last_name) { $sql = "insert into fav_friends values('', '$userid', '$friend_1', '$friend_2', '$friend_3', '$friend_4', '$friend_5', '$friend_6')"; $query = mysql_query($sql); } } and i don't receive any errors, any thoughts? I am very grateful for any input. I have a field in a database that contains references (pattern: authorYYYY), such as the following string:
"authorA1989; authorB1978a; authorB1984; authorC1999; authorC2000a; authorC2000b; authorC2002; authorD2009"
What I want is an output as the following in order to create a proper bibliography (therefore accessing another table and picking out the respective reference details):
authorA
1989
authorB
1978a
1984
authorC
1999
2000a
2000b
2002
authorD
2009
I somewhat fail to group the values (years) per author. I know that keys are unique, but is there a way to work around this?
Many thanks for any help!
This is really cool if I can get it to work My PHP CODE: Code: [Select] $ids = implode (",", $ibforums->input['checkbox']); Grabs from this data in my HTML FORM: Code: [Select] <input name="checkbox[]" type="checkbox" value="{$r['id']},{$r['friendid']}"> Now when I pass my input, I will get 2 different values in both array's how do I separate them out? I know how to seperate them out using implode, but I need to seperate BOTH of the array's that are being passed, and I need to use them separately. I have a really confusing question. I dynamically generate arrays, but for the simplicity of the example we can just use two arrays. Code: [Select] Array 1 ( [0] = 1 [1] = 5 ) Array 2 ( [0] = 2 [1] = 6 ) How can I make the arrays multiple based on the same key value? array 1[0] * array 2[0] = 2 array 1[1] * array 2[1] = 30 I then want to sum the total of the multiplication. $total = 2 + 30; I need to do this to calculate covariance for a statistics procedure I am trying to run but I can't wrap my head around how to do the logic. My array is $arr = array( 'global' => array( array('tabs', 'tabs', 'tabs', array( 'Main' => 'main', 'Gallery' => 'galleryID', 'mera' => 'mera', )), array('main','div', 'div'), array('egice', 'Eice', 'text', '', ''), array('closeHere', 'closeHere', 'closeHere'), array('galleryID','div', 'div'), array('gallery', 'Gallery #1', 'image', '', ''), array('closeHere', 'closeHere', 'closeHere'), array('networking', 'div', 'div'), array('siLype', 'SiLype', 'text', '', ''), array('gpXCge', 'GpXCge', 'toggle_button', '1', 'Yes', 'No', ''), array('closeHere', 'closeHere', 'closeHere'), array('mera', 'div', 'div'), array('maimera', 'Maimera', 'repeatedText', 'resYI', 'GYL',''), array('closeHere', 'closeHere', 'closeHere'), array( 'moures','div', 'div'), array('meLmo', 'MeLmo', 'text', '', ''), array('cYSlot', 'CYSlot', 'text', '', ''), array('closeHere', 'closeHere', 'closeHere'), array('clT', 'clT', 'clT'), ), );
Simply, I want to split the main array to multiple arrays
I had tried here to do it but I can’t I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
Hi, The main idea is that I have a client running on certain desktops. Each 5th minute the client sends and update to the web-server, increasing "tick" by one. Tick start out as 0, so after 5 minutes ticket should be greater than 5. If tick is not greater than 0 then do <code omitted> I Guess I would need some kind of cronjob to complete this, but i'd love to avoid sessions & cookies at all costs, as the same server is checking the same tick but for different accounts.. Any suggestions are highly appreciated ^.^ |