2017-05-29 1 views
-4

Benutzer mit sarkastischen Kommentaren ablehnen, wenn ich die Veröffentlichung gefragt habe, ist es, weil ich es nicht tun konnte.PHP - Native Funktion, um alle Werte eines Arrays nach ihrem Index zu erhalten

Ich muss wissen, ob es eine native PHP-Funktion ist, die mir alle Werte eines Arrays erhalten können, die Indizes Angabe zu erhalten, OHNE CYCLING, zum Beispiel

ich dieses Array haben die Auflistung in eine Funktion:

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; 
} 

Und ich brauche diese Funktion aufzurufen:

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

Und ein Array mit Werten zurück:

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

Wichtig: ich ohne Zyklen dies mit einer nativen PHP-Funktion tun möchten.

Vielen Dank für Ihre Hilfe.

+0

Haben Sie versucht, über Funktionen zu lesen, die ich Ihnen ponted? – splash58

+0

Benutzer mit sarkastischen Kommentaren zurückhalten, wenn ich die Veröffentlichung gefragt habe, weil ich es nicht tun konnte. –

+3

1) Warum. 2) Glauben Sie, dass "native" Funktionen keine Schleifen ausführen? –

Antwort

2

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

+0

'array_intersect_key' verwendet die Werte des ersten Arrays. Wenn Sie Argumente wechseln, ist Ihre Lösung eleganter als meine. – Boldewyn

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

Technisch gesehen, das sind nur native Funktionen ohne PHP-Schleife. Aber natürlich wird PHP unter der Haube mehrere Arrays durchlaufen.

Ich würde diese Lösung im Allgemeinen nicht vorschlagen. Normalerweise ist eine einfache for Schleife effizienter und besser lesbar.

0

Ich habe es auf diese Weise versucht:

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)); 
Verwandte Themen