Вот так
print_r(array_intersect_key($data, ['phone'=>0,'body'=>0]));
Вывод
Array
(
[phone] => 9999999999
[body] => Sample Message
)
Песочница
Или как функция
function getArrayItems(array $array,$items,$separator=','){
if(!is_array($items)) $items = array_filter(array_map('trim',explode($separator,$items)),function($i){
return strlen($i);
});
return array_intersect_key($array, array_flip($items));
}
$data = [
'phone' => '9999999999',
'body' => 'Sample Message',
'caption' => 'Sample',
'filename' => 'Sample File.PDF'
];
print_r(getArrayItems($data, 'body')); // returns [body=>'Sample Message']
print_r(getArrayItems($data, ['body'])); // returns [body=>'Sample Message']
print_r(getArrayItems($data, ['body','caption'])); // returns [body=>'Sample Message','caption' => 'Sample']
print_r(getArrayItems($data, 'body,caption')); // returns [body=>'Sample Message','caption'=>'Sample']
print_r(getArrayItems($data, ' body , caption ')); // returns [body=>'Sample Message','caption'=>'Sample']
print_r(getArrayItems($data, 'body|caption','|')); // returns [body=>'Sample Message','caption'=>'Sample']
Песочница