将2个变量添加到单个数组位置

时间:2011-05-03 15:26:47

标签: php

我正在尝试添加两个变量,这两个变量恰好是一个数组的复选框,该数组是后端的一部分,称为SPIP。

单个复选框的当前阵列设置如下所示:

$UpcomingEvent = 'is_upcoming_event';
$ThermometerEvent = 'is_thermometer_event';
$GLOBALS['champs_extra'] = array (
    'articles' => array (
        $nis => "checkbox|propre|New Inspirational Stories?",
        $UpcomingEvent => "checkbox|propre|Is a Upcoming Event? (<em>present into Home page?</em>)",
        $ThermometerEvent => "checkbox|propre|Add thermometer to event?",
        'redirect_to_hyperlink' => "checkbox|propre|Redirect article to Hyperlink?"
    )
);

$GLOBALS['champs_extra_proposes'] = Array (
    'articles' => Array (
        // tous : par defaut aucun champs extra sur les articles
        'tous' => 'redirect_to_hyperlink',
        // seul le champs extra "new_inspirational_stories" est propos� dans le secteur 42)
        /**
         * UpComming Events
         */
        '44' => $UpcomingEvent,
        '45' => $UpcomingEvent,
)
    );

我想将ThermometerEvent变量添加到数组位置44和45,但无法在不覆盖44或45位置的情况下弄清楚如何操作。

我尝试将它放在另一个数组中,但它不起作用:

'45' => array($UpcomingEvent, $ThermometerEvent), 

感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

什么“不起作用”?你的'45' => array看起来很好。你是如何尝试后来获取这些价值的?

如果你要做

echo $GLOBALS['champs_extra_proposes']['articles']['44'];

你只是得到“数组”,但正在做

echo $GLOBALS['champs_extra_proposes']['articles']['44'][0];

应该为您提供$UpcomingEvent

的价值

答案 1 :(得分:0)

你可以有一个完整的二维数组 - 见这里:

http://www.webcheatsheet.com/PHP/multidimensional_arrays.php

或使用列表:http://php.net/list

<?php

$info = array('coffee', 'brown', 'caffeine');

// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";

// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";

// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>