2017-04-18 3 views
1

I haben wollen, dass variable $total[$i] das Ergebnis der folgenden Funktion zu geben:Verwenden array_reduce und array_map für dynamische Variablen

$total[$i] = (array_reduce((array_map(function($x, $y) { return $x * $y; }, 
        $corp_resp[$i], $corp_resp_template)),function($carry,$item){return $carry+=$item;},0)); 

I aus $corp_resp_template Beispiel erhalten: array (0.4,0.2,0.1) zum $corp_resp array (array (Unter1 (0,2,0.3,0.5) arraysub2 (0.2,0.5,0.7)))

$corp_resp_template für diesen Vorgang ist nur einer. $corp_resp ist Array mit Subarrays innerhalb, die von $carCount in diesem Fall $carCount=2 abhängt, wenn 4 4 Subarrays geben wird, wo diese Werte interpoliert werden mit $corp_resp_template wird nur ein Array der gleichen Größe $corp_resp sein.

Beispiel Operation:

Total 1 = (0,4 * 0,2 + 0,2 * 0,3 + 0,5 * 0,1) = 0,19 $ Summe [0]

Total 2 = (0,4 * 0,2 + 0,2 * 0,5 + 0,1 * 0,7) = 0,25 $ gesamt [1]

Diese Gesamtwerte werden in Zeilen einer Tabelle eingefügt.

Vielen Dank.

+0

Bitte erwähnen. –

Antwort

1

Alles sieht recht Arbeits:

$corp_resp_template = [0.4,0.2,0.1]; 
$corp_resp = [[0.2,0.3,0.5],[0.2,0.5,0.7]]; 

for($i = 0;$i<count($corp_resp);$i++){ 

    $total[$i] = (array_reduce(array_map(function($x, $y){ 
     return $x * $y; 
    },$corp_resp[$i], $corp_resp_template),function($carry,$item){return $carry+=$item;},0)); 
} 

print_r($total); 

aus: klar Ihre Eingabe und erwartete Ausgabe

Array 
(
    [0] => 0.19 
    [1] => 0.25 
)