xProteuSx Posted June 29, 2012 Share Posted June 29, 2012 I have done this before, but it seems that I am doing something wrong this time. I have a function such as this: $numsarray=array(); function checkChores($a) { $match = 0; if (count($numsarray) > 0) { foreach ($numsarray as $chorenum) { if ($chorenum == $a) {$match = 1;} } } if ($match == 0) {array_push($numsarray, $a);} } So if I do the following: checkChores(3); checkChores(4); checkChores(3); When I do this: print_r($numsarray); I should get: Array ( [0] => 3 [1] => 4 ) Instead, I get the following error: Warning: array_push() [function.array-push]: First argument should be an array in /home/pgagnon/public_html/errorlog.php on line 19 This is line 19: {array_push($numsarray, $a);} How is $numsarray not an array??? Help!!! Quote Link to comment Share on other sites More sharing options...
jcanker Posted June 29, 2012 Share Posted June 29, 2012 use square brackets instead of () for the index number Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted June 29, 2012 Author Share Posted June 29, 2012 jcanker, Thanks for the reply, but I have no idea what you are talking about. Nowhere in that code do I refer to particular index. I am familiar with the use of square brackets, but where in my code does that apply? Quote Link to comment Share on other sites More sharing options...
memfiss Posted June 29, 2012 Share Posted June 29, 2012 $numsarray=array(); function checkChores(&$array , $number) { if (!in_array($number,$array)) $array[] = $number ; } checkChores($numsarray,3); checkChores($numsarray,4); checkChores($numsarray,3); var_dump($numsarray); Quote Link to comment Share on other sites More sharing options...
Rage Posted June 29, 2012 Share Posted June 29, 2012 $match = 0; if(count($numsarray) > 0 ){ foreach ($numsarray as $chorenum) { if($chorenum == $a) { $match = 1; } } } else { array_push($numsarray, $a); } Quote Link to comment Share on other sites More sharing options...
jcanker Posted June 29, 2012 Share Posted June 29, 2012 sorry...should've paid closer attention when I looked at it the first time. You're right at that spot. Pass $numsarray in the function with $a. That works, however, you never actually add to the array because it is always length of 0 when it hits the if (count($numsarray) > 0) check, so it never goes into it to get values added to the end. It jumps right to if($match == 0), which will always be true, so the array is empty. This works, but array is empty as noted <?php $numsarray=array(); function checkChores($numsarray,$a) { $match = 0; if (count($numsarray) > 0) { foreach ($numsarray as $chorenum) { if ($chorenum == $a) {$match = 1;} } } if ($match == 0) {array_push($numsarray, $a);} } checkChores($numsarray,3);checkChores($numsarray,4);checkChores($numsarray,3); print_r($numsarray); ?> demo of result at http://betaportal.ankertechnicalservices.com/arraytest.php Quote Link to comment Share on other sites More sharing options...
memfiss Posted June 29, 2012 Share Posted June 29, 2012 <?php $numsarray=array(); function checkChores($numsarray,$a) { $match = 0; if (count($numsarray) > 0) { foreach ($numsarray as $chorenum) { if ($chorenum == $a) {$match = 1;} } } if ($match == 0) {array_push($numsarray, $a);} } checkChores($numsarray,3);checkChores($numsarray,4);checkChores($numsarray,3); print_r($numsarray); ?> ur function 2 times slower then my <? set_time_limit(0); function checkChores(&$numsarray,$a) { $match = 0; if (count($numsarray) > 0) { foreach ($numsarray as $chorenum) { if ($chorenum == $a) {$match = 1;} } } if ($match == 0) {array_push($numsarray, $a);} } function checkChores2(&$array , $number) { if (!in_array($number,$array)) $array[] = $number ; } $ar1 = array(); $ar2 = array(); #start time $time1 = microtime(true); for ($i = 1 ; $i < 999 ; $i++) { checkChores($ar1, mt_rand(1,$i) ); } #end echo "funct 1 : "; echo ($time1 - microtime(true) )."</br>"; #begin funct 2 $time2 = microtime(true); for ($n = 1 ; $n < 999 ; $n++) { checkChores2($ar2, mt_rand(1,$n) ); } #end echo "funct 2 : "; echo ( $time2 - microtime(true)) ; funct 1 : 1.0742318630219 funct 2 : 0.44327902793884 Quote Link to comment Share on other sites More sharing options...
jcanker Posted June 29, 2012 Share Posted June 29, 2012 Uh....it's not my function. It's the originally posted function with the array passed to it when it's called. The original posted question was about why he was getting an error, not how can he make the function more efficient. I'm taking some time to help people solve their error messages and to help people new to PHP become more familiar and comfortable with it. I'm not in the habit of randomly rewriting code when not asked to, and I'm certainly not in the habit of engaging in performance races and chest thumping. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 29, 2012 Share Posted June 29, 2012 Well, as to the original problem: it is due to variable scope. The variable $numsarray is declared outside the function and then inside the function it attempts to reference that variable - which doesn't exist inside the function. Thus the error: Warning: array_push() [function.array-push]: First argument should be an array in /home/pgagnon/public_html/errorlog.php on line 19 The array passed to the function is named as the parameter $a and that is how it should be referenced inside the function. Quote Link to comment Share on other sites More sharing options...
Rage Posted June 29, 2012 Share Posted June 29, 2012 <?php global $array; global $match; $array = array(); $match = "0"; function add_value($a) { global $array; global $match; if(count($array)>0){ $match = "1"; } else { array_push($array, $a); } } add_value("1"); print_r($array); Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 29, 2012 Share Posted June 29, 2012 Don't use globals. Pass the array to the function. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 29, 2012 Share Posted June 29, 2012 Don't use globals. Pass the array to the function. Yeah, right? The array WAS begin passed to the function and that function tried declaring a global instance of the passed array. Makes no sense whatsoever. Quote Link to comment 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.