0

I am new to php and trying to accomplish something that must be easy but for whatever reason I cannot find the answer through a search.

Basically I have an array that contains a few items. I want to print them out in a list but I don't want the array arrows.

Example: using print_r($_SESSION[species]); gives:

Array ( [0] => value1 [1] => value2 [2] => value3 [3] => value4 [4] => )

but I want just:

value1
value2
value3
value4

How can I achieve this? Thank you in advance.

2
  • 1
    Sounds like you want implode. And make sure you quote your array keys: $_SESSION['species']. Commented Oct 20, 2012 at 15:55
  • 1
    you can iterate through an array with a loop (for, foreach, while, pick one) and print out the values. Commented Oct 20, 2012 at 15:59

2 Answers 2

4

You can implode the array

$impl=implode("\n",$_SESSION[species]);
echo $impl

Will give you

value1
value2
value3

This will only insert newlines in HTML source. If you want html linebreaks use

implode("<br />",$tmp);
Sign up to request clarification or add additional context in comments.

Comments

3

...or you could be simpler than the example above, and do a foreach

<?php

foreach($_SESSION['species'] as $value)
{
  echo $value."<br />";
}

?>

1 Comment

Thanks that did it. OK so how could I reverse the list. I.e. I want the last value to be at the top of the list.

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.