Jump to content

Having a problem with array_diff


Shadowing

Recommended Posts

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)); 
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.