I need to use a OR (||) operator in my for() statement but it’s not working as expected.
I send 4 attachments. Two are inline images, the other two are actual attachments.
The problem is that it’s only looping through the two inline images ($results[‘Related’])
I assume my solution is very simple but I am just not seeing it.
Here is my code:
# 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 */
}
}
EDIT: I forgot tell you what the the problem was.
Updated:
There are several ways of doing it, but for maintainability and readability, I would go with an
array_walk()-based solution:Edit:
For older PHP versions that don’t support anonymous functions, you can use a normal function instead: