Answer the question
In order to leave comments, you need to log in
Is there such a function in PHP?
Is there a standard function that extracts only the required values from a given array by keys?
private static function extractOnly(array $parsed, array $params)
{
$result = [];
foreach ($params as $key) {
if (array_key_exists($key, $parsed)) {
$result[$key] = $parsed[$key];
continue;
}
throw new \ErrorException('Key "' . $key . '" do not exists in array');
}
return $result;
}
Answer the question
In order to leave comments, you need to log in
<?php
$list = array("hello", "privet");
$arr = array("hello" => "Vasya", "privet" => "Petya", "zdorovo" => "Misha");
$arr = array_filter($arr, function($key) use ($list) { return in_array($key, $list); } , ARRAY_FILTER_USE_KEY);
print_r($arr);
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question