|
Posted by Jochem Maas on 06/17/05 11:07
Jason Barnett wrote:
> Jay Blanchard wrote:
>
>> [snip]
>> Close... I think array keys are preserved from the (original) first
>> array, but other than that those appear to be the values that should
>> intersect.
>> [/snip]
>>
>> After a var_dump I am seeing that there is an extra space in each
>> element of one array, so no intersection occurs.
>
>
> Well then there you have it... having an extra space means that the
> strings in the first array won't match the strings in the second array.
> Perhaps you can array_intersect(array_walk($array1, 'trim'), $array2)
you'd want array_map not array_walk in this case:
array_intersect(array_map($array1, 'trim'), $array2)
also I found myself writing the following 2 funcs because the std
functions provided don't do what I 'mean'. e.g.:
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
....etc
/**
* array_intersect_keys()
*
* returns the all the items in the 1st array whose keys
* are found in any of the other arrays
*
* @return array()
*/
function array_intersect_keys()
{
$args = func_get_args();
$originalArray = $args[0];
$res = array();
if(!is_array($originalArray)) { return $res; }
for($i=1;$i<count($args);$i++) {
if(!is_array($args[$i])) { continue; }
foreach ($args[$i] as $key => $data) {
if (isset($originalArray[$key]) && !isset($res[$key])) {
$res[$key] = $originalArray[$key];
}
}
}
return $res;
}
/**
* array_diff_keys()
*
* returns the all the items in the 1st array whose
* keys are not found in any of the other arrays
*
* @return array()
*/
function array_diff_keys()
{
$args = func_get_args();
$res = $args[0];
if(!is_array($res)) { return array(); }
for($i=1;$i<count($args);$i++) {
if(!is_array($args[$i])) { continue; }
foreach ($args[$i] as $key => $data) {
unset($res[$key]);
}
}
return $res;
}
>
Navigation:
[Reply to this message]
|