Jump to content

Declaring an Empty Array (with Error)


xProteuSx

Recommended Posts

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!!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.