0

I have created a document in an MongoDB database and I wanted to show the results of the database on a web page with PHP on of the types of the properties of the document is an array that contains string. When I do show the connections this is what shows up

Name: Harry Anderson
Jersey 9
Position Array

The correct position should be "LF", "RF"

The PHP code I am using to get that result is

foreach ($cursor as $obj) {
echo "Name: ". $obj["name"]. "<br/>";
echo "Jersey ". $obj["jersey"]. "<br/>";
echo "Position ". $obj["position"]. "<br/>";
echo "<br/>";
}

I know that I am putting the position object incorrectly and it should be some indication of it being an array

2 Answers 2

1

You just need to implode the array:

foreach ($cursor as $obj) {
  echo "Name    : ".$obj["name"]."<br/>";
  echo "Jersey  : ".$obj["jersey"]."<br/>";
  echo "Position: ".implode(",", $obj["position"])."<br/>";
}

implode(",", array) will create a comma-separated list of all positions.

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

Comments

0

Why not first store it as an array and then iterate over it?

$position = $obj["position"];
foreach ($position as $pos) {
    echo "Position: $pos<br/>";
}

Comments

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.