I have a fo开发者_C百科reach loop like so:
$test_array = array(1, 2, 3, 4, 5);
foreach($categories as $category)
{
echo $category; // outputs cat one cat two cat three cat four cat five etc
echo $test_array; // outputs Array Array Array Array Array
}
and this displays fine and dandy.
But I also want to output the test_array array too but when I do, it says 'Array Array Array Array Array Array ' along with my categories :(
How do I get the test_array to display in my foreach loop too?
If your answer is what you want, and if you're not doing odd stuff with array indexes, then you could say
foreach ($categories as $index => $category)
{
echo $category, $test_array[$index];
}
Note, though, that this depends on both arrays having sequential, numeric indexes. Arrays defined like array(2, 5, 10)
work fine, as do arrays built up by $arr[] = $some_value;
. But if you're using non-numeric keys or adding them out of order, you may have problems.
Use print_r function.
Try this nested loop. I think it'll do what you want.
$loopCount = 0;
foreach($categories as $category) {
echo $category;
if($count <5) {
foreach($test as $someInt) {
echo $someInt;
$count++;
}
}
}
I prefer var_dump personally. Good luck!
Think I came up with a solution:
$test_array = array(1, 2, 3, 4, 5);
$i=-1;
foreach($categories as $category)
{
$i++;
echo $category; // outputs cat one cat two cat three cat four cat five etc
echo $test_array[$i]; // outputs Array Array Array Array Array
}
This works, but is it ok?
精彩评论