tags:

views:

44

answers:

2

Can anyone please tell why this code doesn't work:

$ser = array('one','two','three','four');
$x = serialize($ser);
$x = unserialize($x);
$x = array_push($x, 'five');
$x = serialize($x);
echo $x;

I think it doesn't require any further explanation...

The result is only:

i:5;

+10  A: 

Dont assign the result of array_push to $x.

array_push($x, 'five'); 

This function returns the new number of elements, which you don't want to replace your array.

$ser = array('one','two','three','four'); 
$x = serialize($ser); 
$x = unserialize($x); 
array_push($x, 'five'); 
$x = serialize($x); 
var_dump($x); 
Fosco
And even better use `$x[] = 'five'` :)
nikic
+4  A: 

That's because array_push() returns the number of items in the new array. The value is set by reference.

See the PHP documentation for the array_push function: http://at.php.net/array_push

Prot0