I have a multidimensional array with these values:
array(2) {
[0]=>
array(2) {
[0]=>
string(16) "5"
[1]=>
string(16) "4"
}
[1]=>
array(3) {
[0]=>
string(16) "1"
[1]=>
string(16) "2"
[2]=>
string(16) "3"
}
}
For now the contents of the arrays are only int, but more complex values can be passed through brackets.
However, the number of "sub-arrays" I can varies and can be as small as 1 and large as PHP can handle it.
I wish to loop through the first value of the first array, then the first value of the second array, then the first value of the third array, etc.
Array[0][0] -> Array[1][0] -> Array[2][0]
Array[0][0] -> Array[1][0] -> Array[2][1]
Array[0][0] -> Array[1][1] -> Array[2][0]
Array[0][0] -> Array[1][1] -> Array[2][1]
Array[0][0] -> Array[1][2] -> Array[2][0]
Array[0][0] -> Array[1][2] -> Array[2][1]
Array[0][1] -> Array[1][0] -> Array[2][0]
Array[0][1] -> Array[1][0] -> Array[2][1]
Array[0][1] -> Array[1][1] -> Array[2][0]
Array[0][1] -> Array[1][1] -> Array[2][1]
Array[0][1] -> Array[1][2] -> Array[2][0]
Array[0][1] -> Array[1][2] -> Array[2][1]
// Example with 2 values in each array.
I've been told that a recursive function would do the trick but I have no experience with them and so far I only manage to get partial results.
This is my current code
// $array - Multidimensional array above
// $ini - Current array - ex: $array[0]
// $ini - Number of main arrays left - ex: count($array)
// $result - string that houses the temporary value
// $finalResult - Array that combines all the string when there is nothing left
function r ($array, $ini, $int, $result, $finalResult)
{
for ($i = 0; $i < count($array[$ini]); $i++)
{
$result = $array[$ini][$i] . ',' . $result;
if ($int != 0)
{
$result = $this->r($array, ++$ini, --$int, $result, $finalResult);
}
else
{
$finalResult[] = $result;
}
}
return $finalResult;
}
This is what I get from it
array(2) {
[0]=>
string(22) "2,Array"
[1]=>
string(39) "3,2,Array"
}
// I managed at some point get the values separate but, sadly
The desired outcome of the function is that the values be displayed in this matter:
array(2) {
[0]=>
string(16) "5, 1"
[0]=>
string(16) "5, 2"
[0]=>
string(16) "5, 3"
[0]=>
string(16) "4, 1"
[0]=>
string(16) "4, 2"
[0]=>
string(16) "4, 3"
}
Any guidance in the right direction would be greatly appreciated.
Thanks in advance!