Hi everybody,
I know I'm still a noob with php, but I feel so sad I couldn't manage such a simple (at least apparently) issue.

Here's what I want to achieve, in general:
- I'm reading XML files via simple_xml
- while reading I would populate an array with some strings from the XMLs, if some specific conditions are satisfied
- as I need to read several XML files, I must populate the array from within a function

Easy, isn't it? Unfortunately, not for me...
When I try to pass an array to the function, the array is not recognized.

here's a sample:

$array = array();
	$array[0] = "ciccio";
	$array[1] = "coccio";
	$array[2] = "pasticcio";
	$array[3] = "astuccio";
	
	$n = 5;
	
	function push_array($a,$in){
		array_push($a,$in);
	}
	
	for(i=0; i<$n; i++){
		push_array($array,$i);
		print_r($array);
		echo "<br/>";
	}

and here it is the result:
Array ( [0] => ciccio [1] => coccio [2] => pasticcio [3] => astuccio )
Array ( [0] => ciccio [1] => coccio [2] => pasticcio [3] => astuccio )
Array ( [0] => ciccio [1] => coccio [2] => pasticcio [3] => astuccio )
Array ( [0] => ciccio [1] => coccio [2] => pasticcio [3] => astuccio )
Array ( [0] => ciccio [1] => coccio [2] => pasticcio [3] => astuccio )

:(
Where am I wrong?

Thanx evrybody,
S

Hey,

You start your for loop with i = 0, rather than $i = 0. When you come to push $i onto the array, it therefore doesn't exist.

for(i=0; i<$n; i++){
push_array($array,$i);

Also, try putting error_reporting on:

ini_set( 'error_reporting', E_ALL );

Finally, just for simplicity sake in the future... when adding elements to an array, you can do this:

$arrExample[] = 'This';
$arrExample[] = 'is';
$arrExample[] = 'my';
$arrExample[] = 'example';

// Then, if you wanted to print "is" to the screen...

echo $arrExample[1];

When you get to having large arrays, its sometimes easier not to have to provide an index yourself.

R.

Why did you define push_array( $array, $in ) { array_push( $array, $in )} ?

Unless you're unless you're planning to add additional functionality into push_array before you call array_push, you're just changing the name of a core function.

$n = 5;

for($i=0; $i<$n; $i++)
{
     array_push($array,$i);
     print_r($array);
     echo "<br/>";
}
Array ( 
           [0] => ciccio 
           [1] => coccio
           [2] => pasticcio 
           [3] => astuccio
           [4] => 0
           [5] => 1
           [6] => 2
           [7] => 3
           [8] => 4
 );

This completely eliminates the need for the user defined function.

You start your for loop with i = 0, rather than $i = 0. When you come to push $i onto the array, it therefore doesn't exist.

Sorry, that was my mistake: I copied the code from the wrong file, but I already corrected that, when writing. Otherwise the print_r($array);
would'nt have printed the command five times.

Also, try putting error_reporting on:

ini_set( 'error_reporting', E_ALL );

thanks, that will be useful in the future

Finally, just for simplicity sake in the future... when adding elements to an array, you can do this:

$arrExample[] = 'This';
$arrExample[] = 'is';
$arrExample[] = 'my';
$arrExample[] = 'example';
// Then, if you wanted to print "is" to the screen...
echo $arrExample[1];

Yes I know that method, but it is not working, anyway.
Neither it is working this one:
$arrExample["string"]="other_string";
which, actually, is the one I need, in order to retrieve array values I need later in the script.

When you get to having large arrays, its sometimes easier not to have to provide an index yourself.

As I said above, what I need in the end is an array with named IDs.
Every method for populating arrays is perfectly working putting the code directly into the loop. The problem, I guess, is in the way I have to pass the array from a function to another, which isn't clear to me yet, at all.

Thanks for your time, anyway. I do appreciate that
s

Why did you define push_array( $array, $in ) { array_push( $array, $in )} ?

I defined that function to represent the structure that I have in my script and to make it easily readable.
Actually within the push_array() function, there are many other things happening. But I would know why the basic code is not working, even if I write it down getting rid of all the rest.
I'm pretty sure the problem is in passing the array's variable to the function. I just would understand why.

I know that the code you wrote works. But I can't avoid the function inbetween.

Thanks for replying and for your time,
s

This article has been dead for over six months. Start a new discussion instead.