Shadowing Posted June 12, 2012 Share Posted June 12, 2012 hey guys im having problem getting array_diff to work. when I print_r on array1 it looks like ( [0] => 210643 [1] => 210641 [2] => 210648 [3] => 210650 ) my largest problem is i dont even know what the 2nd array looks like. I cant figure out how to see it. I'm checking check boxes on one screen then sending ito to a php page via ajax. The array works with my script using a foreach statement but now im trying to do more with it. im thinking the array looks like this (210643,210641,210648) so with out the keys $var = array_diff(array1,array2); my results in this is null. it makes $var null and if I flip the arrays around then it just makes $var equal array 1 I wish i could see what array2 looks like as an array how can i return this back to my page so i can alert with javascript? i hate how blind i am at the moment where i cant see what this 2nd array looks like. function remove() { $return = $_POST['association']; echo json_encode(array("display" => $return)); } Quote Link to comment https://forums.phpfreaks.com/topic/264027-having-a-problem-with-array_diff/ Share on other sites More sharing options...
Psycho Posted June 12, 2012 Share Posted June 12, 2012 You are making this way more difficult than it needs to be. I always advise to build any functionality without AJAX. Once you have all the basic pieces working, THEN add AJAX to add the immediate feedback. We have no way to know what the array looks like since you have not shown how the fields are build and/or how the data is transferred via the AJAX call. There are some easy ways to verify how the data is structured. Here is one way: in the AJAX call have the PHP processing page do a simple print_r() on the passed value. Then have the AJAX return function populate a div with the return value. Or, even better make the PHP processing page do a var_dump() of the passed value - that should give you all the data you need to understand what is being passed. Quote Link to comment https://forums.phpfreaks.com/topic/264027-having-a-problem-with-array_diff/#findComment-1353062 Share on other sites More sharing options...
Shadowing Posted June 12, 2012 Author Share Posted June 12, 2012 Thanks for the reply Psycho when I do var_dump or print_r error: trips that i set up stating not retreiving data so my php script is failing. the array does exist cause it works in the foreach statement. but now this is driving me crazy on why i cant simply output it back on my page to see the array obvious stuff is already checked the ajax return does work. I did $retun = 1; and it displayed it as a alert i set up on the page also i did impode on $_POST['association'] and shot it back to my page and it worked. function remove_saved_planet() { $return = var_dump($_POST['association']); echo json_encode(array("display" => $return)); } what is even more crazy is this decided to just rebuild the array so this way i know what its going to look like foreach($_POST['association'] AS $val) { $auto_id = (int) $val; $remove_save = "DELETE FROM save_planet WHERE auto_id= '".($auto_id)."'"; mysql_query($remove_save) or trigger_error("SQL", E_USER_ERROR); $array2[0] = $auto_id; } $array1 = $_SESSION['saved_planets']; $_SESSION['saved_planets'] = array_diff(array1,$array2); I could just delete the values from the array while its looping. driving me crazy on why this isnt working ha. Quote Link to comment https://forums.phpfreaks.com/topic/264027-having-a-problem-with-array_diff/#findComment-1353067 Share on other sites More sharing options...
Shadowing Posted June 12, 2012 Author Share Posted June 12, 2012 ahh i solved my problem lol not sure why i missed it decided to implode it one more time and the return wasnt what i expected so the array_diff was working as it should with the situation of what array2 contained but still though why cant i var_dump it, maybe the json_encode is messing it up? Quote Link to comment https://forums.phpfreaks.com/topic/264027-having-a-problem-with-array_diff/#findComment-1353068 Share on other sites More sharing options...
Shadowing Posted June 12, 2012 Author Share Posted June 12, 2012 Also i was thinking is this the best way to delete more then one row from the data base? is there a way to do it where the query isnt in the loop? and make mysql do all the work? Quote Link to comment https://forums.phpfreaks.com/topic/264027-having-a-problem-with-array_diff/#findComment-1353069 Share on other sites More sharing options...
Psycho Posted June 12, 2012 Share Posted June 12, 2012 Also i was thinking is this the best way to delete more then one row from the data base? No. is there a way to do it where the query isnt in the loop? and make mysql do all the work? Yes. Take the array and implode it into a comma separated list. Then use the IN clause for MySQL. If the values are numeric then you don't need to do anything fancy, but if they are string values then they would need to be enclosed in quotes. But, of course, you shoudl always validate/sanitize the inputs. So, assuming these are integer values the following would work. //Create a "clean" array of integers. // array_map() applies the intval() function to each value to ensure they are all integers // array_filter() removes any empty/null values from the array $autoIDsAry = array_filter(array_map('intval', $_POST['association'])); //Convert resultign array to string of comma-separated values $autoIDsSQL = implode(', ', $autoIDsAry); //Create and run query to delete all records from original array $query = "DELETE FROM save_planet WHERE auto_id IN ({$autoIDsSQL})"; mysql_query($remove_save) or trigger_error("SQL", E_USER_ERROR); NEVER run queries in loops. Quote Link to comment https://forums.phpfreaks.com/topic/264027-having-a-problem-with-array_diff/#findComment-1353077 Share on other sites More sharing options...
Shadowing Posted June 13, 2012 Author Share Posted June 13, 2012 Thanks alot psycho i'll start doing that instead Quote Link to comment https://forums.phpfreaks.com/topic/264027-having-a-problem-with-array_diff/#findComment-1353398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.