Foreach ends in null instead of six objects

<?php

$justmove = array(0, 1, 2, 3, 4, 5);

$empty = array();
// $empty = new Array(); or new array(); doesn't work

$i = 0;

foreach ($justmove as $i) {
	//global $justmove, $empty, $matchIndex; doesn't work
	$i++;

	$idontcare[$i]["whatever1"] = $justmove[0];
	$idontcare[$i]["whatever2"] = $justmove[1];
	$idontcare[$i]["whatever3"] = $justmove[2];

	$empty = array_push($empty, $justmove[2]);
	// &$empty doesn't work
}

var_dump($empty);

?>

Is what I have. Should return 2, 2, 2, 2, 2, 2 (as array objects). Returns null instead.

How would I fix that?

The errors (interpreter might be cutting further messages off):

PHP Warning: array_push() expects parameter 1 to be array, integer given in /home/IRIfGj/prog.php on line 18
PHP Warning: array_push() expects parameter 1 to be array, null given in /home/IRIfGj/prog.php on line 18
PHP Warning: array_push() expects parameter 1 to be array, null given in /home/IRIfGj/prog.php on line 18
PHP Warning: array_push() expects parameter 1 to be array, null given in /home/IRIfGj/prog.php on line 18
PHP Warning: array_push() expects parameter 1 to be array, null given in /home/IRIfGj/prog.php on line 18

Don’t redefine empty as a variable. Just use the array_push part.

	array_push($empty, $justmove[2]);
1 Like

Nope. If I don’t define array, it would be literally null. Now the error makes sense “parameter 1 to be array, null given”. That doesn’t solve anything, but actually makes the error.

Have a look at the docs:

Thus $empty is indeed an integer… just reread @Drummin’s reply. ;-)

1 Like

(removed)

This is what Drumming means. Just tested this.


$justmove = array(0, 1, 2, 3, 4, 5);

$empty = array();
// $empty = new Array(); or new array(); doesn't work

$i = 0;

foreach ($justmove as $i) {
	//global $justmove, $empty, $matchIndex; doesn't work
	$i++;

	$idontcare[$i]["whatever1"] = $justmove[0];
	$idontcare[$i]["whatever2"] = $justmove[1];
	$idontcare[$i]["whatever3"] = $justmove[2];
	array_push($empty, $justmove[2]);
}

print_r($empty);

Result:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.