Re: Re: If I have 2 arrays...

From: Date: Fri, 17 Jun 2005 08:07:01 +0000
Subject: Re: Re: If I have 2 arrays...
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-217121@lists.php.net to get a copy of this message
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;
}


« previous php.general (#217121) next »