2012-08-27 83 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本身作爲保證進行設置。