2012-08-27 87 views
0

我需要在for()语句中使用OR(||)运算符,但它不像预期的那样工作。使用for()来评估多个项目

我发送4个附件。两个是内嵌图像,另外两个是实际附件。

的问题是,它只能通过两个内置图片循环($结果[“相关”])

我认为我的解决方案很简单,但我只是没有看到它。

这里是我的代码:

# 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 */ 
    } 
} 

编辑:我忘了告诉你了什么问题。

+1

您遇到了什么问题?没有理由不能在你的陈述中使用'||'。不知道你的问题是否来自于在没有设置或没有设置的东西上运行count()。 – BAF

+0

啊,我在发表评论之前没有看到您的编辑。 – BAF

回答

1

更新时间:

有这样做的几种方法,但对于可维护性和可读性,我会继续array_walk()基于一种解决方案:

$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'); 
} 
+0

我看不出这对我有用。我需要循环它,因为'$'后面的'for()'语句中有更多的代码。 – Draven

+0

@Draven:那么这是一个非常重要的细节,你完全忽略了你的问题,你不觉得吗? :-) – FtDRbwLXw6

+0

是的,我很抱歉。 – Draven

2

单独做。

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'])); 
    } 
} 
+0

这不一定等价,但我怀疑这是OP实际需要的东西 – tobyodavies

+0

@tobyodavies是的,除OP之外省略'$ filename'后面的代码在'$ filename = ...'之后。 – xdazz

+0

是的,我做了很多代码,我宁愿不重复。 – Draven

0

你需要总结一下吗?

我猜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'])); 
    } 
} 
+1

这不会工作,因为elseif() – Draven

+0

糟糕,好抓。固定。 – BAF

+0

我知道这将无法正常工作,因为$ attachment_type。但我看到你在做什么,我应该能够通过添加更多的if()语句来工作 – Draven

0

您呼叫count的东西,没有设置,算了算$attachment_type本身作为保证进行设置。