crump;11011449 wrote:
When I do a check to see if an image exist would I do in_array or string comparison like strcmp?
To compare arrays, you use array comparison. To compare strings, you use string comparison. Usually. It all depends on for what reason YOU need to compare them. More on this later.
crump;11011449 wrote:
Or would I check to see if a1.gif, for example, is in_array?
If you want to know if it exists in the array, then of course you will have to check if it's in the array. So if you have some images in one array and want to see what images from another array are there, you have to check if they are there... The filenames are strings, so to see if one filename matches another filename, string comparison is used. To see what is present (or absent) from one array in another array where the elements contain these strings, array comparison is performed using string comparison on the individual elements.
Let's say you have two small boxes (in real life). One contains 3 fruits, and the other also contain 3 fruits. How do YOU (not a computer) see if a single fruit is the same as another fruit? How do you check if each box has the same contents as the other box? Now, the computer has to do the exact same thing, but it can only look at one single object at a time and compare it against one other single object at a time (your eyes may take in more things simultaenously).
As for being more specific on determining if one fruit is the same as another fruit, YOU may, just like you do when comparing things using a computer, set up whatever criteria you want to! An apple may be the same as another apple. Or a red apple may be the same as a red strawberry. Or a red apple may not be the same as a green apple. Or a Granny Smith (green apple) may not be the same as an Aromatic Russet (another green apple).
crump;11011449 wrote:
Are these just strings, array of strings, or arrays?
These what? An array is an array, no matter what it contains. A string is a string, no matter if it's stored directly in a "normal" variable, in an array or in a constant.
define('CONSTANT_CONTAINTING_THREE_AS_INT', 3);
define('SAME_BUT_STRING', '3');
$array1 = array(1, 'two', CONSTANT_CONTAINTING_THREE_AS_INT, 'four', 5.5);
$array2 = array('alpha', 'bravo', SAME_BUT_STRING);
$array3 = array($array1, $array2);
function get_type($val)
{
if (is_string($val))
return 'string';
if (is_int($val))
return 'integer';
if (is_double($val))
return 'double';
if (is_bool($val))
return 'boolean';
if (is_array($val))
return 'array';
if (is_null($val))
return 'null';
if (is_object($val))
return get_class($val);
if (is_resource($val))
return 'resource';
}
foreach ($array1 as $v)
{
printf("%-8s(%s)%s",
$v, get_type($v), PHP_EOL
);
}
echo PHP_EOL;
foreach ($array2 as $v)
{
printf("%-8s(%s)%s",
$v, get_type($v), PHP_EOL
);
}
echo PHP_EOL;
foreach ($array3 as $v)
{
printf("%-8s(%s)%s",
$v, get_type($v), PHP_EOL
);
}
Do note that in the above case, you have two types for the value 3. 3 is an integer, while '3' is a string. Both echo simply as 3 (without the quotes).
So how are arrays compared? Well, as with the apples, it's up to you. The way array_diff works is to use a string comparison, as per the documentation for [man]array_diff[/man]
Note:
Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
Thus, to know if two elements will be considered equal, cast them to strings and see for yourself.
$a1 = array(1, 2, 3.0, 4);
$a2 = array(1, '2', '3', 'four');
# What will array diff yield for array_diff($a1, $a2) and for array_diff($a2, $a1)?
# Well, cast to strings and print
echo 'elements of $a1 cast to string:'.PHP_EOL;
foreach ($a1 as $v)
{
echo (string) $v . "\t";
}
echo PHP_EOL.'elements of $a2 cast to string:'.PHP_EOL;
foreach ($a2 as $v)
{
echo (string) $v . "\t";
}
echo PHP_EOL.PHP_EOL;
$diff = array_diff($a1, $a2);
print_r($diff);
echo PHP_EOL.PHP_EOL;
$diff = array_diff($a2, $a1);
print_r($diff);
Thus, if you store basenames in both arrays, array_diff will work in this case. If you store complete path info, it will not (since the paths differ). You would then have to either strip the path info from each array element before using array_diff, or you'd have to use another comparison method which only compares basename (which means writing it yourself).