I
I
iron eagle2017-11-24 15:47:12
PHP
iron eagle, 2017-11-24 15:47:12

Sum two elements of two different arrays and put the result into a variable in php?

I have two arrays with elements:
$array[1] // this is where the number 10 is stored, when I echo the result it shows 10
$array2[1] // this is where the number 20 is stored, when I echo the result it shows 20
My code , like:
$summa = $array[1] + $array2[1]; // - shows the result 10
If before $summa, assign array1 to a variable and array2 to another variable and add them to $summa - the result will be generally 00 ( echo "$summa"; // shows 0 )
These values ​​are entered into arrays through code execution :
get_file_contents...
preg_match
Maybe that's why I can't add them together? Maybe they are not stored in numeric data, but as a string?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DrunkMaster, 2017-11-24
@DrunkMaster

$array2[1] // the number 20 is stored here, when I echo the result it shows 10

What am I doing wrong? https://ideone.com/7vbKoq

D
Denis A., 2017-11-25
@myphpscript

$summa = (int)$array[1] + (int)$array2[1];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question