我正在尝试检查用户从 $_POST 提交的数据是否至少具有与我传递的数组相同的元素。我这样做是因为稍后我将通过调用 $_POST['element'] 使用这些元素,而且我不喜欢有关该元素不存在(未设置)的错误。 :)
我不想使用像 isset($_POST['x'], $_POST['y'], $_POST['z']) 这样的东西,因为每次我需要重写 $_POST,它似乎也不可读。
我尝试使用 in_array(array('x', 'y', 'z'), $_POST),但它不起作用(它返回 false 当它应该返回 true 时)。任何想法如何使它工作? :) 我确定我有空字符串,如 $_POST['x']、$_POST['y'] 和 $_POST['z ']。我什至尝试将软管三个 $_POST 元素的值更改为空字符串以外的其他内容 - 仍然...无法按预期工作。 :(
多谢指教! :)
编辑:
刚刚发现 in_array() 检查值,而不是键。然后,我尝试这样做......
in_array(array('title', 'slug', 'content'), array_keys($_POST))
它仍然返回 false。怎么会这样? ;/
编辑#2:
好的,这是调试的结果......
传入 $_POST:
array(3) {
["title"]=>
string(0) ""
["slug"]=>
string(0) ""
["content"]=>
string(0) ""
}
array_keys($_POST) 的结果:
array(3) {
[0]=>
string(5) "title"
[1]=>
string(4) "slug"
[2]=>
string(7) "content"
}
in_array(array('title', 'slug', 'content'), array_keys($_POST)) 的结果:
bool(false)
问题...为什么它是 false?据我所知,我都做对了。
编辑#3:
最后,我创建了自己的方法 Arr::keys_exists($keys, $array) .
最佳答案
in_array() 检查数组中是否存在值,而不是键。如果你想检查一个键是否存在,那么你会想要类似...
in_array('x', array_keys($_POST));
或者更简单的...
array_key_exists('x', $_POST);
如果您想一次检查多个键:
$required_keys = array('x'=>1, 'y'=>1, 'z'=>1);
$missing_keys = array_diff_key($required_keys, $_POST);
$missing_keys_count = count($missing_keys);
关于php - 为什么 in_array() 对 $_POST 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7698252/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用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
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou