-1

I want to use array_diff with the result of an mysql query and the result of an $REQUEST here is what I tried:

while($resultarray3 = mysql_fetch_array($result3)) 
{
$Bestand = $resultarray3['Bestand']
}
$Ergebnis = array_diff($_REQUEST['Menge'], $Bestand);

I got this error by using it: Warning: array_diff(): Argument #2 is not an array in /var/www/html/lager_management/warenkorb.php on line 143

Example for the array $Bestand:

Array ( [0] => 20 [1] => 250 [2] => 90 ) 

Example for the array $Menge:

Array ([0] => 10 [1] => 45 [3] => 80 )
4
  • 1) Why are you not doing this in the SQL query to begin with, 2) yeah, because $Bestand is not an array, it's just the last value from the database. Commented Nov 27, 2012 at 8:14
  • Hi, see the PHP manual for the correct syntax of array_diff(). the seconed argument ($Bestand in ur case) must also be an array. so change it to $Bestand = array(resultarray3['Bestand']); Commented Nov 27, 2012 at 8:15
  • @AyyappanSekar i knew this and used also array thats not working Commented Nov 27, 2012 at 8:19
  • @Pgr456: Hi, it seems u r getting a value from $_REQUEST array. it must also be an array.. check that... Commented Nov 27, 2012 at 8:30

2 Answers 2

0

You are replacing the varible each time in loop. Try this

$Bestand =array(); while($resultarray3 = mysql_fetch_array($result3)) { $Bestand[] = $resultarray3['Bestand'] } $Ergebnis = array_diff($_REQUEST['Menge'], $Bestand);

Need to change/Modify

$Bestand=array()

and

$Bestand[] = $resultarray3['Bestand']
4
  • When i print_r the array Ergebniss i get the values from $_REQUEST['Menge'] and not the values from the array_diff Commented Nov 27, 2012 at 8:18
  • array_diff gives the difference between two arrays. Can you please edit your post and add possible array OR you can refer php.net/manual/en/function.array-diff.php for help Commented Nov 27, 2012 at 8:23
  • I saw your code ,array_diff returns an array containing all the entries from array1 that are not present in any of the other arrays. Your both the array's are different it will return $_REQUEST['Menge'] as a result Commented Nov 27, 2012 at 8:31
  • Actually this is expected behavior of array_diff If you want to test it you can swipe parameters $Ergebnis = array_diff($Bestand,$_REQUEST['Menge']); at this time you will get $Bestand if you print_r($Ergebnis); Commented Nov 27, 2012 at 8:36
0

use the array, not simple var

$Bestand[] = $resultarray3['Bestand'];
4
  • i tried this too gives me same result Commented Nov 27, 2012 at 8:15
  • what result is var_dump($Bestand); before $Ergebnis = array_diff($_REQUEST['Menge'], $Bestand); Commented Nov 27, 2012 at 8:18
  • the var_dump is : array(3) { [0]=> string(2) "20" [1]=> string(3) "250" [2]=> string(2) "90" } Commented Nov 27, 2012 at 8:21
  • $Ergebnis = array_diff($_REQUEST['Menge'], array()); return same result (Warning: array_diff(): Argument #2 is not an array in )? Commented Nov 27, 2012 at 8:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.