如果标题有点困惑,请见谅。
我下面的结果指出了正在发生的事情。
这个问题已经变得非常广泛,所以我在这里强调这两件事:
看我的 更新 在底部,出于合理的原因。
整个文件: (见第 86 行)http://codepad.org/oIXaZZaB
这是发生了什么:
我有这个数组,它包含每个语言代码的计数整数。
$downloads = [
'all' => [
'nl-BE' => 0,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 0,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
$_GET['langCode'] 中检索到的 key 参数,总是 获取 计数 到 $downloads['all'] 中的相应键.$langCode前。如果他没有用过同样的$langCode之前,计数也加到$downloads['unique']中对应的key .$uniqueVisitors = [
'127.0.0.1' => [ 'en-UK' ]
];
if ($uniqueVisitors == null)
{
$uniqueVisitors = [
$ipNew => []
];
}
$countUnique = 1;
/* Check Unique Visitors */
if (isset($uniqueVisitors[$ipNew]))
{
if (in_array($langCode, $uniqueVisitors[$ipNew]))
{
$countUnique = 0;
}
else $uniqueVisitors[$ipNew][] = $langCode;
}
else $uniqueVisitors[$ipNew] = [ $langCode ];
/* Update Data */
$downloads['all'][$langCode]++;
if ($countUnique)
{
$downloads['unique'][$langCode]++;
}
/* Save Data */
file_put_contents('data/unique-visitors.json', json_encode($uniqueVisitors));
file_put_contents('data/downloads.json', json_encode($downloads));
$langCode只包含一个键(例如 'nl-NL' )'en-UK' , 'en'也经常被计算在内。'fr-FR' 也是如此和 'fr-BE' .'nl-NL'和 'nl-BE' .0 时/* Retrieved first from JSON file */
$downloads = [
'all' => [
'nl-BE' => 0,
'nl-NL' => 2,
'fr-BE' => 1,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 0,
'nl-NL' => 1,
'fr-BE' => 1,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
$uniqueVisitors = [
'127.0.0.1' => [ 'nl-NL', 'fr-BE' ]
];
/* Result after running script with 'en-UK' langCode */
$downloads = [
'all' => [
'nl-BE' => 0,
'nl-NL' => 2,
'fr-BE' => 1,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 1,
'en' => 1 // Why is this one counted ?
],
'unique' => [
'nl-BE' => 0,
'nl-NL' => 1,
'fr-BE' => 1,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 1,
'en' => 1 // Why is this one counted ?
]
];
$uniqueVisitors = [
'127.0.0.1' => [ 'nl-NL', 'fr-BE', 'en', 'en-UK' ] // 'en' stored as well ?
];
file_put_contents() 将更新的数据保存回他们的文件时功能,只需进行一些测试和转储,整个计数确实按预期工作!file_put_contents()用于保存(正确)更新数据的函数会以某种方式干扰,并在执行时更改某些计数。json_encode()在我的 file_put_contents()函数,我收到“数组到字符串的转换”错误,两次 用于保存我的计数数据( $downloads ):Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.php on line 89
Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.php on line 90
Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.php on line 90
file_put_contents('data/unique-visitors.json', json_encode($uniqueVisitors));
file_put_contents('data/downloads.json', json_encode($downloads)); // This one is called twice!
$downloads数据正确显示更新后的值。file_put_contents()存储更新的 $downloads数据,它会混淆更新的值。即使我在使用 file_put_contents 之前/之后转储更新的数据,显示乱七八糟。$downloads大批:$downloads = [
'all' => [
'nl-BE' => 0,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 0,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
$_GET['langCode'] = 'nl-BE' 更新值:$downloads = [
'all' => [
'nl-BE' => 1,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 1,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
file_put_contents() 存储更新的数组时:$downloads = [
'all' => [
'nl-BE' => 2, // Why this increment?
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 1,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
file_put_contents 时, 更新 $downloads在同一脚本运行(之前和之后 file_put_contents )上转储时,显示更新不正确。我什至不必使用 file_get_contents检索错误保存的数据。<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)/?$ index.php?lang=$1 [QSA,NC,L]
</IfModule>
最佳答案
我也无法重现您的错误。在我看来,您的脚本被调用了两次(请参阅“全部”中的项目是递增的,但不是“唯一”中的。file_put_contents()没有问题.您可以通过在脚本中添加以下内容来验证这一点:
<?php
mail('you@domain.tld', 'script executed at '.time(), '');
// your code ...
php yourscript.php
关于php - file_put_contents() 弄乱了更新的数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20372587/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样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]
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数