Search code examples
phparraysarray-maparray-reduce

Fatal error: Unsupported operand types in PHP


I have that code where i want to multiply corp_resp for corp_resp_template and sum dynamically.

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

Code:

 $valor[] = array();
   foreach ( $_POST as $key => $value ){

       $valor[] = $value;

corp_resp Output:

array(17) { [0]=> array(0) { } [1]=> string(4) "0.00" [2]=> string(4) "0.00" [3]=> string(4) "0.00" [4]=> string(4) "0.00" [5]=> string(4) "0.50" [6]=> string(4) "0.00" [7]=> string(4) "0.00" [8]=> string(4) "0.00" [9]=> string(4) "0.00" [10]=> string(4) "0.50" [11]=> string(4) "0.00" [12]=> string(4) "0.00" [13]=> string(4) "0.00" [14]=> string(4) "0.00" [15]=> string(4) "0.00" [16]=> string(4) "0.00" } 

Code:

$corp_resp_template = array();

while ($mostrar = mysql_fetch_array($consulta)) {

$corp_resp_template[] = $mostrar['corp_resp_template'];
}

corp_resp_template Output:

array(17) { [0]=> string(4) "0.15" [1]=> string(4) "0.15" [2]=> string(4) "0.15" [3]=> string(4) "0.15" [4]=> string(4) "0.15" [5]=> string(4) "0.15" [6]=> string(4) "0.15" [7]=> string(4) "0.15" [8]=> string(4) "0.15" [9]=> string(4) "0.15" [10]=> string(4) "0.15" [11]=> string(4) "0.15" [12]=> string(4) "0.15" [13]=> string(4) "0.15" [14]=> string(4) "0.15" [15]=> string(4) "0.15" [16]=> string(4) "0.15" } 

Is giving the following error: Fatal error: Unsupported operand types in C: Where the 2 are arrays.

Another doubt is how to work when i have corp_resp_template and i want to do the result for diferents corp_resp in the same function?


Solution

  • That was solved with array_shift function. The format array was diferent in both variables it why was giving that error.