I am aware there are various questions regarding this topic but most are involving associative arrays etc. I need to add 2 arrays/matrices together and receive the resultant value.
ie:
<?php
$matrixa = array(
array($a1,$b1),
array($c1, $d1)
);
$matrixb = array(
array($a2,$b2),
array($c2, $d2)
);
for ($i=0; $i<2; $i++){
for ($j=0; $j<2; $j++){
$matresult[$i][$j] = $matrixa[$i][$j] + $matrixb[$i][$j];
}
}
//which essentially produces:
// $matrixc = array(
// array($matrixa[0][0]+$matrixb[0][0], $matrixa[0][1]+ $matrixb[0][1]),
// array($matrixa[1][0]+$matrixb[1][0], $matrixa[1][1]+ $matrixb[1][1])
// );
?>
Matrix C will be used for some sort of user input verificiation but my question is, how can I print matrixA and matrixB so that they appear in the following format within a line of HTML code:
[x, y] or x y
[x, y] x y
Something like:
<p>What is <?php echo $matrixa; ?> + <?php echo $matrixb; ?> ?</p>
This statement currently outputs 'What is ARRAY + ARRAY ?'
@RomanPerekhrest EDIT - I am utilising Mathjax in my code so I was wondering if there was a method that was less verbose than:
<p>What is `[[
<?php echo $matrixa[0][0]; ?> <?php echo $matrixa [0][1];?>
],[
<?php echo $matrixa[1][0]; ?> <?php echo $matrixa [1][1];?>
]]` + `[[
<?php echo $matrixb[0][0]; ?> <?php echo $matrixb [0][1];?>
],[
<?php echo $matrixb[1][0]; ?> <?php echo $matrixb [1][1];?>
]]` ?
</p>
There is (almost) always a less verbose method of doing things.
<?php
// functions for outputting inner and outer structure of matrices
$inner = function($in){return '[ '.implode(' ', $in).' ]';};
$outer = function($in) use ($inner){return implode(',', array_map($inner, $in));};
?>
<p>
<?php echo 'What is `['.$outer($matrixa).']` + `['.$outer($matrixb). ']` ?'; ?>
</p>
Question is, if it is worth doing for such a small dataset. Second thing is code readability / understandability. One instance of solution like this in a project is forgivable, almost cute even, but when this becomes a standard, it can be a living hell for non-authors of the code, and in time, for you as well.