Given an array of strings, such as $a:
$a = array("zero", "one", "cat");
I’m looking to create array $b, populated “along the diagonal” with values from $a:
$b[0] = ["zero", "-", "-"]
$b[1] = ["-", "one", "-"]
$b[3] = ["-", "-", "cat"]
So far, I have:
function matrix($m, $n, $value) {
return array_fill(0, $m, array_fill(0, $n, $value));
} // create a matrix (m,n) of $value
$a = array("zero", "one", "cat");
$b = matrix(count($a),count($a),"'-'"); // create $b, filled with '-'
for($i = 0; $i < count($a); $i++){
$b[$i][$i] = $a[$i];
} // fill matrix b with strings from a, along the diagonal
print_r($b);
In practice $a is going to be quite large, so I’m looking for a method with the lowest chance of bringing a server to its knees.
(Extra thanks given if you explain your version as if I were 8 years old.)
A method that “does not bring a server to its knees” in my opinion would avoid function calls and would also not use recursion, even though both may look cleaner in terms of code. Either way, this is a fairly simple problem.
First, we create our dashes array:
You may notice a pattern based on dimensions:
Specifically, the X and Y coordinates match along the diagonal. That’s handy because we only have to know one or the other to know the slot to insert into our dashes array. The index of the item in
$aqualifies as either (and both):