如何在函数中为通过引用传递的参数创建多个PHP数组? [英] How do I create multiple PHP arrays in a function for parameters passed by reference?
问题描述
如果我在php中有一个函数可以通过解析xml创建多个对象数组,那么如何返回这些数组作为引用?
我需要调用new吗?分配数组?如何在函数中定义它们?
function ParseConfig($ rawxml,& $ configName,& $ radioArr,& ; $ flasherArr,& $ irdArr)
对不起,我的意思是返回多个数组作为参数引用。 / p>
如何在函数内部创建数组?还是我可以开始将其用作数组?
我没有注意到您编辑了问题,这改变了一切完全。您在这里有两个不错的选择:
-
在函数顶部明确更改它们:
function foo($ bar,& $ baz,& $ qux){
$ baz = $ qux = array();
}
-
也许这是引入对象的好地方吗?
class parseConfig {
protected $ 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(empty($ this-> rawXml)){
返回null;
}
$ this-> configName =’’;
$ this-> radioArr =
$ this-> flasherArr =
$ this-> irdArr = array();
//解析在此处进行,可能返回false
返回true;
}
公共函数setRawXml($ raw_xml){
$ this-&raw; rawXml = $ raw_xml;
返回$ this;
}
公共函数getRawXml(){
return $ this-> rawXml;
}
公共函数getRadioArr(){
return $ this-> radioArr;
}
公共函数getFlasherArr(){
return $ this-> flasherArr;
}
公共函数getIrdArr(){
return $ this-> 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?
I hadn't noticed that you edited your question, which changes things entirely. You have two decent options here:
Explicitly change them at the top of the function:
function foo($bar, &$baz, &$qux) { $baz = $qux = array(); }
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; } }
这篇关于如何在函数中为通过引用传递的参数创建多个PHP数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!