0

I have a foreach 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?

5
  • You mean display all the elements each time, or one per category? Commented Apr 26, 2011 at 22:57
  • 1
    Could you post the code you used that outputted 'Array Array etc'? Commented Apr 26, 2011 at 22:59
  • @cHao display the categories array and the test_array both Commented Apr 26, 2011 at 23:08
  • @Keith: That doesn't really answer the question. You want something like "cat one 1 2 3 4 5 cat two 1 2 3 4 5", or "cat one cat two.... 1 2 3 4 5"? Commented Apr 26, 2011 at 23:48
  • @cHao: stackoverflow.com/questions/5797436/… like this, this seems to work great! Commented Apr 26, 2011 at 23:52

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Use print_r function.

2 Comments

Or better yet, var_dump so you can see types and values that type juggle to empty strings (like false)
Anyway I can output the test_array in a "normal way"?
1

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++;
        }
      }
    }

2 Comments

Thanks, anyway to limit the test_array being called 5 times?
See the edit above. I added a loop counter. Is that what you want it to do?
0

I prefer var_dump personally. Good luck!

2 Comments

Anyway I can output the test_array in a "normal way"?
you can output a <pre> tag before your call to var_dump. This will make the output preformatted. Also, if you "view source" in the browser, it'll show you a nice preformatted output.
0

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?

1 Comment

Depends on result which you want to achieve :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.