Skip to content

Added function Arr::extract_existing() #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.4/develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion classes/Kohana/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,31 @@ public static function extract($array, array $paths, $default = NULL)

return $found;
}


/**
* Retrieves multiple keys from an array only if they exist.
*
* // Get the values "a", "b" from $arr = array('a' => 'foo', 'b' => 'bar', c => 'car')
* $result = Arr::extract_existing($arr, array('a', 'b'));
* returns array('array('a' => 'foo', 'b' => 'bar')
*
* @param array array to filter keys from
* @param array list of key names to search for
* @return array
*/
public static function extract_existing($array, array $keys)
{
$found = array();
foreach ($keys as $key)
{
if(isset($array[$key]))
{
$found[$key] = $array[$key];
}
}
return $found;
}

/**
* Retrieves muliple single-key values from a list of arrays.
*
Expand Down