如何在函数中为通过引用传递的参数创建多个 PHP 数组?

发布于 2024-12-04 16:08:27 字数 284 浏览 0 评论 0 原文

如果我在 php 中有一个函数可以通过解析 xml 创建多个对象数组,那么如何将这些数组作为引用返回?

我需要调用 new 来分配数组吗?如何在函数中定义它们?

function ParseConfig($rawxml, &$configName, &$radioArr, &$flasherArr, &$irdArr)

抱歉,我的意思是返回多个数组作为参数引用。

我该如何在函数内创建数组?或者我可以开始将它用作数组吗?

If I have a function in php that creates several arrays of objects from parsing xml, how do I return those arrays as references?

Do I need to call new to allocate the arrays? How do I define them within the function?

function ParseConfig($rawxml, &$configName, &$radioArr, &$flasherArr, &$irdArr)

Sorry, I mean return multiple arrays as parameter references.

what do I do to create the array inside the function? or can I just start using it as an array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秋叶绚丽 2024-12-11 16:08:28

在这种情况下不需要使用引用。 PHP 使用写入时复制机制,该机制还跟踪指向当前值的项目数。如果您从函数返回一个值,并将该结果分配给变量,则引用计数仍仅为 1,因为函数中使用的变量将在函数返回时被销毁。您可以安全地编辑包含函数返回值的变量,而不必担心浪费内存。

示例测试:

function getaz() { 
    $array = range('a','z'); 
    echo '2: ', memory_get_usage(), "\n";   
    return $array; 
}

echo '1: ', memory_get_usage(), "\n";
$p = getaz();
echo '3: ', memory_get_usage(), "\n";
$p[0] = '3';
echo '4: ', memory_get_usage(), "\n";
$p = array_merge($p, range('A','Z'));
echo '5: ', memory_get_usage();

输出:

1: 337304 [initial]
2: 340024 [inside function, value is created]
3: 340072 [outside function, value is assigned]
4: 340072 [element modified but remains same size]
5: 342696 [array extended]

如果我将函数更改为通过引用返回,我会得到以下信息:

1: 337312 [it took 8 bytes more memory to define the function]
2: 340032 [accounting for the extra 8 bytes, no change]
3: 340080 [accounting for the extra 8 bytes, no change]
4: 340080 [accounting for the extra 8 bytes, no change]
5: 342704 [accounting for the extra 8 bytes, no change]

希望这有帮助!

有关如何以及为何以这种方式工作的更多阅读,请查看 这个无耻的博客插件解释了一些关于 PHP 如何处理变量和值的内容

There is no need to use references in this case. PHP uses a copy on write mechanism which also keeps track of the number of items pointing to the current value. If you return a value from a function, and assign that result to a variable, the refcount will still only be one, since the variable used in the function will be destroyed when the function returns. You can safely edit the variable containing the value returned from the function without worrying about wasting memory.

Sample test:

function getaz() { 
    $array = range('a','z'); 
    echo '2: ', memory_get_usage(), "\n";   
    return $array; 
}

echo '1: ', memory_get_usage(), "\n";
$p = getaz();
echo '3: ', memory_get_usage(), "\n";
$p[0] = '3';
echo '4: ', memory_get_usage(), "\n";
$p = array_merge($p, range('A','Z'));
echo '5: ', memory_get_usage();

Output:

1: 337304 [initial]
2: 340024 [inside function, value is created]
3: 340072 [outside function, value is assigned]
4: 340072 [element modified but remains same size]
5: 342696 [array extended]

If I change the function to return by reference, I get the following:

1: 337312 [it took 8 bytes more memory to define the function]
2: 340032 [accounting for the extra 8 bytes, no change]
3: 340080 [accounting for the extra 8 bytes, no change]
4: 340080 [accounting for the extra 8 bytes, no change]
5: 342704 [accounting for the extra 8 bytes, no change]

Hope this helps!

For some more reading on how and why this works this way, check out this shameless blog plug that explains a little bit about how PHP deals with variables and values.

默嘫て 2024-12-11 16:08:28

return &$array;

但只返回 $array 恕我直言就可以了

return &$array;

But it is fine to just return the $array IMHO

晚风撩人 2024-12-11 16:08:28

我没有注意到你编辑了你的问题,这完全改变了事情。这里有两个不错的选择:

  1. 在函数顶部显式更改它们:

    function foo($bar, &$baz, &$qux) {
        $baz = $qux = array();
    }
    
  2. 也许这是引入对象的好地方?

    class parseConfig {
        受保护的$rawXml;
        受保护的$configName;
        受保护的$radioArr = array();
        受保护的 $flasherArr = array();
        受保护的 $irdArr = array();
    
        公共函数 __construct($raw_xml = null) {
            if (!is_null($raw_xml)) {
                $this->rawXml = $raw_xml;
                $this->parse();
            }
        }
    
        公共函数解析($raw_xml = null){
            if (!is_null($raw_xml)) {
                $this->rawXml = $raw_xml;
            }
    
            if (空($this->rawXml)) {
                返回空值;
            }
    
            $this->configName = '';
            $this->radioArr =
            $this->flasherArr =
            $this->irdArr = array();
    
            // 解析发生在这里,可能返回 false
    
            返回真;
        }
    
        公共函数 setRawXml($raw_xml) {
            $this->rawXml = $raw_xml;
            返回$这个;
        }
    
        公共函数 getRawXml() {
            返回 $this->rawXml;
        }
    
        公共函数 getRadioArr() {
            返回 $this->radioArr;
        }
    
        公共函数 getFlasherArr() {
            返回 $this->flasherArr;
        }
    
        公共函数 getIrdArr() {
            返回 $this->irdArr;
        }
    }
    

I hadn't noticed that you edited your question, which changes things entirely. You have two decent options here:

  1. Explicitly change them at the top of the function:

    function foo($bar, &$baz, &$qux) {
        $baz = $qux = array();
    }
    
  2. Perhaps this is a good place to introduce an object?

    class parseConfig {
        protected $rawXml;
        protected $configName;
        protected $radioArr   = array();
        protected $flasherArr = array();
        protected $irdArr     = array();
    
        public function __construct($raw_xml = null) {
            if (!is_null($raw_xml)) {
                $this->rawXml = $raw_xml;
                $this->parse();
            }
        }
    
        public function parse($raw_xml = null) {
            if (!is_null($raw_xml)) {
                $this->rawXml = $raw_xml;
            }
    
            if (empty($this->rawXml)) {
                return null;
            }
    
            $this->configName = '';
            $this->radioArr   =
            $this->flasherArr =
            $this->irdArr     = array();
    
            // parsing happens here, may return false
    
            return true;
        }
    
        public function setRawXml($raw_xml) {
            $this->rawXml = $raw_xml;
            return $this;
        }
    
        public function getRawXml() {
            return $this->rawXml;
        }
    
        public function getRadioArr() {
            return $this->radioArr;
        }
    
        public function getFlasherArr() {
            return $this->flasherArr;
        }
    
        public function getIrdArr() {
            return $this->irdArr;
        }
    }
    
蒲公英的约定 2024-12-11 16:08:28

从函数返回的任何数组都将通过引用返回,直到您修改该数组为止,在此之前将创建该数组的副本:

$myArray = functionThatReturnsArray(); //$myArray is a reference to the array returned
echo $myArray[0]; //still a reference
$myArray[0] = 'someNewValue'; //from here $myArray will be a copy of the returned array

Any array returned from a function will be returned by reference until you modify that array before which a copy of the array will be made:

$myArray = functionThatReturnsArray(); //$myArray is a reference to the array returned
echo $myArray[0]; //still a reference
$myArray[0] = 'someNewValue'; //from here $myArray will be a copy of the returned array
雨的味道风的声音 2024-12-11 16:08:28

您可以在参数列表中指定数组类型(自 PHP 5.1 起),如果这样做,您可以立即开始将其用作数组:

function my_function( array &$arr ) {
    $arr[0] = 'someValue';
}

如果不这样做,您应该在函数的顶部进行检查:

function my_function( &$arr ) {
    if( !is_array($arr) ) $arr = array(); // or generate an error or throw exception
    $arr[0] = 'someValue';
}

You can specify the array type in the argument list (since PHP 5.1), if you do that you can start using it as an array right away:

function my_function( array &$arr ) {
    $arr[0] = 'someValue';
}

If you don't you should make a check at the top of your function:

function my_function( &$arr ) {
    if( !is_array($arr) ) $arr = array(); // or generate an error or throw exception
    $arr[0] = 'someValue';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文