简体   繁体   English

传递当前数组作为参考[PHP / Drupal6]

[英]Pass current array as reference [PHP/Drupal6]

I have a variable $doctype that is an array: 我有一个变量$doctype是一个数组:

$doctype = array(
    'news' => ('news'),
    'documents' => ('documents'),
    'forms' => ('forms'),
    'other' => ('other'),
);

which is used as options for another array: 它用作另一个数组的选项:

$form['doc_type'] = array(
    '#title' => 'Document Type',
    '#type' => 'radios',
    '#options' => $doctype,
    '#size' => '30',
    '#required' => TRUE,
    '#default_value' => $doctype['news'],

now I'm trying to pass the current value for another function 现在我正试图将当前值传递给另一个函数

I've tried: 我试过了:

form($form_state, &$doctype) {

but it shows up as a missing argument 但它显示为一个缺失的论点

I want to pass it through a reference, not as a return (already occupied/don't want to work around it) 我想通过引用传递它,而不是作为返回(已经占用/不想解决它)

Any help is appreciated 任何帮助表示赞赏

Assuming form() specifies its second argument as a reference, then you shouldn't pass &$doctype as a parameter. 假设form()指定其第二个参数作为引用,则不应将&$doctype作为参数传递。 If the function specifies a parameter as a reference, then all you need to do is pass that variable and it will get passed as a reference. 如果函数将参数指定为引用,那么您需要做的就是传递该变量,它将作为引用传递。

Eg 例如

<?php
    function form($var1, &$var2) {
        $var2[2] = 5;
    }
    $var2 = array(1,2,3,4);
    form('test', $var2);
    echo $var2[2]; // Echoes '5';
?>

However, I'm again assuming that form() is built into drupal, in which case as far as I know there's no way of passing $doctype as a reference without changing the core code that defines form() 但是,我再次假设form()内置于drupal中,在这种情况下,据我所知,没有办法将$doctype作为引用传递而不更改定义form()的核心代码

Out of curiosity, why do you need to pass it as a reference? 出于好奇,你为什么要把它作为参考传递? Can you clarify "already occupied/don't want to work around it" 你能澄清一下“已经占据/不想解决它”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM