Voting

: five plus one?
(Example: nine)

The Note You're Voting On

t dot wiltzius at insightbb dot com
20 years ago
I needed to compare an array with associative keys to an array that contained some of the keys to the associative array. Basically, I just wanted to return only a few of the entries in the original array, and the keys to the entries I wanted were stored in another array. This is pretty straightforward (although complicated to explain), but I couldn't find a good function for comparing values to keys. So I wrote this relatively straightforward one:

<?php

function key_values_intersect($values,$keys) {
foreach(
$keys AS $key) {
$key_val_int[$key] = $values[$key];
}
return
$key_val_int;
}

$big = array("first"=>2,"second"=>7,"third"=>3,"fourth"=>5);
$subset = array("first","third");

print_r(key_values_intersect($big,$subset));

?>

This will return:

Array ( [first] => 2 [third] => 3 )

<< Back to user notes page

To Top