tags:

views:

42

answers:

1

Tried examples from 'php.net' but don't understand what's the problem. Any suggestions ?

<?php

$_SESSION['test'] = array('a' => '1', 'b' => '2');

foreach ($_SESSION['test'] as $key => $val)
    echo "key: " . $key . " val: " . $val . "\n";

// Parse error
array_push($_SESSION['test']['c'] => '3'); 

// Parse error
$_SESSION['test'][] = ('c' => '3');

foreach ($_SESSION['test'] as $key => $val)
    echo "key: " . $key . " val: " . $val . "\n";

?>
+4  A: 

Is this what you are looking for?

$_SESSION['test']['c'] = '3';

[] is designed to append to a numeric-key array. If you used this on an associative array, it will result in an index of (largest numeric-key + 1).

St. John Johnson
Yes, thanks a lot :--)
dygi
No problem! In the future, `array_push` works the same way as `[]` except the first parameter is the array and the second is the value.
St. John Johnson