0

我需要在我的 for() 语句中使用 OR (||) 运算符,但它没有按预期工作。

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

问题是它只循环通过两个内联图像($results['Related'])

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

这是我的代码:

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

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

4

4 回答 4

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']));
  }
}
于 2012-08-27T02:49:23.793 回答
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');
}
于 2012-08-27T03:11:57.077 回答
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']));
    }
}
于 2012-08-27T02:51:33.633 回答
0

您正在调用count未设置的内容,只需将$attachment_type其视为已设置的内容即可。

于 2012-08-27T02:54:06.937 回答