我知道这类问题已经被问了很多,但我还没有找到如何按照我想要的方式去做。
所以,基本上我有这个数组。
array(7) {
["site"]=>
array(5) {
["production"]=>
bool(false)
["url"]=>
string(29) "http://localhost/"
["name"]=>
string(6) "Sitename"
["title"]=>
string(7) ": Index"
["pagedata"]=>
array(1) {
["default"]=>
string(5) "Index"
}
}
["DB"]=>
array(5) {
["host"]=>
string(9) "localhost"
["user"]=>
string(4) "root"
["pass"]=>
string(4) "secret"
["database"]=>
string(12) "database"
["engine"]=>
string(7) "eMySQLi"
}
["cache"]=>
array(2) {
["file"]=>
array(1) {
["time"]=>
int(500)
}
["memcache"]=>
array(2) {
["my_first_vps_ip"]=>
string(17) "my_first_vps_port"
["my_second_vps_ip"]=>
string(18) "my_second_vps_port"
}
}
["skin"]=>
array(2) {
["name"]=>
string(9) "thehabbos"
["mobile"]=>
array(2) {
["enabled"]=>
bool(true)
["name"]=>
string(16) "mobile_thehabbos"
}
}
["lang"]=>
array(1) {
["name"]=>
string(7) "English"
}
["widget"]=>
array(1) {
["default"]=>
string(7) "Icecron"
}
["cron"]=>
array(1) {
["DatabaseBackup"]=>
array(1) {
["execute_every"]=>
int(86400)
}
}
}
所以,如果我使用像这样的方法“解析”这个数组(将数组转换成一个对象)..
private function parse($arr)
{
foreach ($arr as $key => $val)
{
$this->{$key} = is_array($val) ? $this->parse($val) : $val;
}
return $this;
}
我最终会得到类似...
object(Configure)#3 (27) {
["production"]=>
bool(false)
["url"]=>
string(29) "http://localhost/RevFramework"
["name"]=>
string(7) "English"
["title"]=>
string(7) ": Index"
["default"]=>
string(7) "Icecron"
["pagedata"]=>
*RECURSION*
["site"]=>
*RECURSION*
["host"]=>
string(9) "localhost"
["user"]=>
string(4) "root"
["pass"]=>
string(4) "root"
["database"]=>
string(12) "rev_database"
["engine"]=>
string(7) "eMySQLi"
["DB"]=>
*RECURSION*
["time"]=>
int(500)
["file"]=>
*RECURSION*
["my_first_vps_ip"]=>
string(17) "my_first_vps_port"
["my_second_vps_ip"]=>
string(18) "my_second_vps_port"
["memcache"]=>
*RECURSION*
["cache"]=>
*RECURSION*
["enabled"]=>
bool(true)
["mobile"]=>
*RECURSION*
["skin"]=>
*RECURSION*
["lang"]=>
*RECURSION*
["widget"]=>
*RECURSION*
["execute_every"]=>
int(86400)
["DatabaseBackup"]=>
*RECURSION*
["cron"]=>
*RECURSION*
}
所以,我可以像这样使用它..
echo $this->url;
但我想做的是像这样使用它..
echo $this->site->url;
如果可能的话,我有什么办法可以做到这一点吗?
最佳答案
您需要为数组值实例化一个新对象以设置属性。除非您有更具体的内容,否则 stdClass 将执行以下操作:
private function parse(array $arr, stdClass $parent = null) {
if ($parent === null) {
$parent = $this;
}
foreach ($arr as $key => $val) {
if (is_array($val)) {
$parent->$key = $this->parse($val, new stdClass);
} else {
$parent->$key = $val;
}
}
return $parent;
}
关于php - 多维数组转对象,具体方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8410300/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信