Php 使用for()评估多个项目
我需要在for()语句中使用OR(| |)运算符,但它没有按预期工作 我发送了4个附件。两个是内联图像,另外两个是实际附件 问题是它只在两个内联图像中循环($results['Related')) 我认为我的解决方案很简单,但我只是没有看到它 这是我的密码:Php 使用for()评估多个项目,php,Php,我需要在for()语句中使用OR(| |)运算符,但它没有按预期工作 我发送了4个附件。两个是内联图像,另外两个是实际附件 问题是它只在两个内联图像中循环($results['Related')) 我认为我的解决方案很简单,但我只是没有看到它 这是我的密码: # Check for attachments if(isset($results['Related']) || isset($results['Attachments'])) { if(isset($results['Relate
# Check for attachments
if(isset($results['Related']) || isset($results['Attachments']))
{
if(isset($results['Related']))
{
$attachment_type = $results['Related'];
}
elseif(isset($results['Attachments']))
{
$attachment_type = $results['Attachments'];
}
for($i = 0; ($i < count($results['Attachments']) || $i < count($results['Related'])); $i++)
{
# Format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
$filename = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $attachment_type[$i]['FileName']));
/* LOTS MORE CODE HERE */
}
}
#检查附件
if(isset($results['Related'])| | isset($results['Attachments']))
{
如果(isset($results['Related']))
{
$attachment_type=$results['Related'];
}
elseif(isset($results['Attachments']))
{
$attachment_type=$results['Attachments'];
}
对于($i=0;($i
编辑:我忘了告诉你问题出在哪里。分开做
if(isset($results['Related']) {
foreach ($results['Related'] as &$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
}
}
if(isset($results['Attachments']) {
foreach ($results['Attachments'] as &$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
}
}
分开做
if(isset($results['Related']) {
foreach ($results['Related'] as &$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
}
}
if(isset($results['Attachments']) {
foreach ($results['Attachments'] as &$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
}
}
你需要计算总数吗 我猜
count($results['Attachments'))
是2,而count($results['Related'))
也是2,因为你说你每个发送两个。在这种情况下,它只会运行前两次
听起来你需要这样的东西:
# Check for attachments
if(isset($results['Related']) || isset($results['Attachments']))
{
$count = 0;
if(isset($results['Related']))
{
$attachment_type = $results['Related'];
$count += count($results['Related']);
}
if(isset($results['Attachments']))
{
$attachment_type = $results['Attachments'];
$count += count($results['Attachments']);
}
for($i = 0; $i < $count; $i++)
{
# Format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
$filename = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $attachment_type[$i]['FileName']));
}
}
#检查附件
if(isset($results['Related'])| | isset($results['Attachments']))
{
$count=0;
如果(isset($results['Related']))
{
$attachment_type=$results['Related'];
$count+=count($results['Related']);
}
如果(isset($results['Attachments'))
{
$attachment_type=$results['Attachments'];
$count+=count($results['Attachments');
}
对于($i=0;$i<$count;$i++)
{
#格式化文件名(将空格改为下划线,然后删除任何不是字母、数字或下划线的内容)
$filename=preg\u replace('/[^0-9,a-z,\,'.]*/i','',str\u replace('''.'''.',$attachment\u type[$i]['filename']);
}
}
您需要对计数求和吗
我猜count($results['Attachments'))
是2,而count($results['Related'))
也是2,因为你说你每个发送两个。在这种情况下,它只会运行前两次
听起来你需要这样的东西:
# Check for attachments
if(isset($results['Related']) || isset($results['Attachments']))
{
$count = 0;
if(isset($results['Related']))
{
$attachment_type = $results['Related'];
$count += count($results['Related']);
}
if(isset($results['Attachments']))
{
$attachment_type = $results['Attachments'];
$count += count($results['Attachments']);
}
for($i = 0; $i < $count; $i++)
{
# Format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
$filename = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $attachment_type[$i]['FileName']));
}
}
#检查附件
if(isset($results['Related'])| | isset($results['Attachments']))
{
$count=0;
如果(isset($results['Related']))
{
$attachment_type=$results['Related'];
$count+=count($results['Related']);
}
如果(isset($results['Attachments'))
{
$attachment_type=$results['Attachments'];
$count+=count($results['Attachments');
}
对于($i=0;$i<$count;$i++)
{
#格式化文件名(将空格改为下划线,然后删除任何不是字母、数字或下划线的内容)
$filename=preg\u replace('/[^0-9,a-z,\,'.]*/i','',str\u replace('''.'''.',$attachment\u type[$i]['filename']);
}
}
您正在对未设置的内容调用count
,只需将$attachment\u type
本身计算为保证设置的内容。您正在对未设置的内容调用count
,只需将$attachment\u type
本身计算为保证设置的内容。更新:
有几种方法可以做到这一点,但为了可维护性和可读性,我会选择基于的解决方案:
$doLotsOfStuff = function(&$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
// Your other code goes here.
};
if (isset($results['Related'])) {
array_walk($results['Related'], $doLotsOfStuff);
}
if (isset($results['Attachments'])) {
array_walk($results['Attachments'], $doLotsOfStuff);
}
编辑:
对于不支持匿名函数的旧PHP版本,您可以使用普通函数:
function doLotsOfStuff(&$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
// Your other code goes here.
}
if (isset($results['Related'])) {
array_walk($results['Related'], 'doLotsOfStuff');
}
if (isset($results['Attachments'])) {
array_walk($results['Attachments'], 'doLotsOfStuff');
}
更新: 有几种方法可以做到这一点,但为了可维护性和可读性,我会选择基于的解决方案:
$doLotsOfStuff = function(&$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
// Your other code goes here.
};
if (isset($results['Related'])) {
array_walk($results['Related'], $doLotsOfStuff);
}
if (isset($results['Attachments'])) {
array_walk($results['Attachments'], $doLotsOfStuff);
}
编辑:
对于不支持匿名函数的旧PHP版本,您可以使用普通函数:
function doLotsOfStuff(&$el) {
$el['FileName'] = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $el['FileName']));
// Your other code goes here.
}
if (isset($results['Related'])) {
array_walk($results['Related'], 'doLotsOfStuff');
}
if (isset($results['Attachments'])) {
array_walk($results['Attachments'], 'doLotsOfStuff');
}
你看到的问题是什么?没有理由不能在for语句中使用
|
。不确定您的问题是否源于对未设置或未设置的内容运行count()。啊,在发布该评论之前,我没有看到您的编辑。您看到的问题是什么?没有理由不能在for语句中使用|
。不确定您的问题是否源于对未设置或未设置的内容运行count()。啊,在发布该评论之前,我没有看到您的编辑。这不一定是等效的,但我怀疑这就是OP的实际内容wants@tobyodavies是的,只不过OP在$filename=…
之后使用$filename
省略了代码。这不一定是等效的,但我怀疑OP实际上就是这样做的wants@tobyodavies是的,只不过OP在$filename=…
之后使用$filename
省略了代码。我知道由于$attachment\u类型,这也不起作用。但是我知道你在做什么,我应该能够通过添加更多if()语句来实现这一点。我知道,由于$attachment_类型,这也不起作用。但是我知道你在做什么,我应该可以通过添加更多if()语句来实现这一点。我不知道这对我来说会如何。我需要循环它,因为在$filename
行之后的for()
语句中有更多的代码。@Draven:这是一个非常重要的细节,您完全没有考虑到,不是吗?:-)没问题。在这种情况下,我建议您使用。我将用一个例子更新我的答案。@Draven:我用一个array\u walk()
example更新了答案。我希望这能有所帮助。我在$doLotsOfStuff=FUNCTION(&$el)
行上看到语法错误,意外的T_函数(&$code>):(我不知道这对