开发者

How do I properly add key => val pair to an existing array [closed]

开发者 https://www.devze.com 2022-12-24 17:39 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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";

?>


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消