Shame to ask, but I dont know how to echout all array's keys in this foreach cycle:
$i=0;
foreach ($matches as $match) {
echo $match[$i++];
}
Right now result is just "1". Instead of 1 2 3 7
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 7
)
)
If $matches is always an array with a single element you can take a shortcut and use:
foreach ($matches[0] as $match) {
echo $match.' ';
}
Othewise use nested foreach loops
foreach($matches as $match) {
foreach($match as $element) {
echo $element.' ';
}
}
That's because your code is essentially echoing the first item of the first array, the second of the second, etc, but since you only have one array in the "main" one, it outputs that single 1
. While this would be useful for proving that there are infinitely many real numbers, it's not what you're aiming for!
Try this:
foreach($matches as $match) {
echo implode(" ",$match);
}
You just want the arrays keys? use
array_keys()
You want to echo the keys while looping? use
foreach($array AS $key=>$value){
echo $key.' ';
}
You want to echo the values? use:
foreach($array AS $key=>$value){
echo $value.' ';
}
Otherwise, you need to explain what you are actually trying to do.
You can use array_walk_recursive to accomplish this.
$array = array(
array(
0 => 1,
1 => 2,
2 => 3,
3 => 7
)
);
function print_out($item, $key) {
echo "$key = $item
";
}
array_walk_recursive($array, 'print_out');
Output:
0 = 1
1 = 2
2 = 3
3 = 7