2017-05-29 84 views
-4

如果我發佈了問題,請用戶諷刺性地評論,這是因爲我無法做到。PHP - 通過索引獲取數組的所有值的本地函數

我需要知道,如果有一個本地的PHP函數,可以讓我得到一個數組的所有值,指定索引的方式,而不自行車,例如

我有這個數組列表中功能:

function get_mime($index) 
{ 
    $data = array(
     'jpg' => 'image/jpeg', 
     'png' => 'image/png', 
     'gif' => 'image/gif', 
     'zip' => 'application/x-compressed', 
     'doc' => 'application/msword', 
     'dot' => 'application/msword', 
     'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
     'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 
     'docm' => 'application/vnd.ms-word.document.macroEnabled.12', 
     'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', 
     'xls' => 'application/vnd.ms-excel', 
     'xlt' => 'application/vnd.ms-excel', 
     'xla' => 'application/vnd.ms-excel', 
     'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
     'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 
     'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', 
     'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', 
     'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', 
     'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 
     'ppt' => 'application/vnd.ms-powerpoint', 
     'pot' => 'application/vnd.ms-powerpoint', 
     'pps' => 'application/vnd.ms-powerpoint', 
     'ppa' => 'application/vnd.ms-powerpoint', 
     'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 
     'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 
     'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 
     'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 
     'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 
     'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 
     'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12' 
    ); 
    return $data; 
} 

,我需要調用這個函數:

get_mime(array('jpg', 'png', 'gif')); 

並返回與值的數組:

array('image/jpeg', 'image/png', 'image/gif') 

重要:我想用一個本地的PHP函數來做到這一點,而無需使用週期。

非常感謝您的幫助。

+0

您是否嘗試過閱讀,我ponted你的功能呢? – splash58

+0

如果我發表了要求的出版物,諷刺性地評論用戶,這是因爲我做不到。 –

+3

1)爲什麼。 2)你認爲「原生」函數不執行循環嗎? –

回答

2

$ res = array_intersect_key(array_flip(['jpg','png','gif']),$ data);

+0

'array_intersect_key'使用第一個數組的值。如果你改變參數,你的解決方案比我的更優雅。 – Boldewyn

0
function get_mime($index) 
{ 
    $data = array(
     //... 
    ); 
    return array_values(
     array_intersect_key(
      $data, array_combine(
       $index, array_fill(
        0, count($index) 
       ) 
      ) 
     ) 
    ); 
} 

從技術上講,這只是本地函數不帶PHP循環。但是,當然,在PHP的底層,PHP會循環遍歷兩個數組。

我不會建議這個解決方案。通常一個簡單的for循環更高效,更具可讀性。

0

我已經嘗試過這樣:

function get_mime($indices) 
{ 
    $data = array(
     'jpg' => 'image/jpeg', 
     'png' => 'image/png', 
     'gif' => 'image/gif', 
     'zip' => 'application/x-compressed', 
     'doc' => 'application/msword', 
     'dot' => 'application/msword', 
     'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
     'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 
     'docm' => 'application/vnd.ms-word.document.macroEnabled.12', 
     'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', 
     'xls' => 'application/vnd.ms-excel', 
     'xlt' => 'application/vnd.ms-excel', 
     'xla' => 'application/vnd.ms-excel', 
     'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
     'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 
     'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', 
     'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', 
     'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', 
     'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 
     'ppt' => 'application/vnd.ms-powerpoint', 
     'pot' => 'application/vnd.ms-powerpoint', 
     'pps' => 'application/vnd.ms-powerpoint', 
     'ppa' => 'application/vnd.ms-powerpoint', 
     'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 
     'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 
     'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 
     'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 
     'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 
     'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 
     'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12' 
    ); 
    $init = array_intersect_key($data, array_flip($indices)); 
    $init = array_values ($init); 
    return $init; 
} 

$indices = array('jpg', 'png'); 
echo print_r(get_mime($indices));