0

Can somebody please tell me where/what I'm doing it wrong and how the conversion should take place? I'm new to PHP and MongoDB so please excuse my naivety...

This is my document in mongo:
{ "_id" : "x", "links" : [1,2,3] }

In PHP, I do this:
foreach($cur as $obj) echo $obj['_id'] . "-->" . $obj['links']

My output is:
x-->Array
instead of this:
x-->1,2,3

Thanks in advance!

2 Answers 2

1

Just to add clarification to the above answer.. what he's doing is really taking an array and casting it into a string with a ',' as a separator.. since the $obj is coming down as an array you would need to cycle through it (unless you're fine displaying it as such).

foreach($obj['links'] as $link){
   //do something
}

This way you're keeping it in an array format rather than using an operation to cast it to a string.

0
0

Try this:

echo $obj['_id'] . "-->" .  implode(',',$obj['links']);
3
  • awesome! it works great. can you also tell me what I should do to store them in a separate array at my application layer? Commented Sep 16, 2011 at 8:08
  • dont worry about it, casting it to an array worked! thanks a lot again! Commented Sep 16, 2011 at 8:12
  • All right, $a=$obj['links'];(makes a copy of the array) and $a=&$obj['links']; (references the original array) Commented Sep 16, 2011 at 8:20

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.