2

I have the following find (from mongodb) field working well but when I try to use the preg_replace, I get the following error message

Notice: array to string conversion in ...

My code:

$mongorow = $collection->findOne(array('_id' => new MongoId($id))); //finds based on ID
$dotodot  = preg_replace("/_DOT_/",  ".", $mongorow);
1
  • Try: $dotodot = preg_replace("/_DOT_/", ".", implode('', $mongorow)); Commented Feb 6, 2014 at 14:34

3 Answers 3

3

The problem is that $mongorow is an array and you are treating it like a string or a variable.

it should be:

$dotodot = preg_replace("/_DOT_/",  ".", $mongorow['_id']);
Sign up to request clarification or add additional context in comments.

3 Comments

pls how do i treat it as an array, I just need to replace the value everywhere it exists in the array. sorry i'm a bit new to this :)
if i do: $dotodot = preg_replace("/_DOT_/", ".", $mongorow['_id']); no error message but i think it's searching id fields. the field I plan on making these changes is: $dotodot = preg_replace("/_DOT_/", ".", $mongorow['properties']);
i think the issue is that the field pattern exists in is a hash field. how can i convert this to string please?
0

findOne() returns an array (or NULL if search failed), so you'll have to first get the field out of your result before you treat it as a String.

$str = $mongorow['whateverYouWereLookingFor'];
$dotodot = preg_replace("/_DOT_/",  ".", $str);

Edit: If you need to replace across a whole array, you want to look at the array_map() function.

1 Comment

by $str = $mongorow['whateverYouWereLookingFor'];? is 'whateverYouWereLookingFor' referring to the field I wish to make the replacement in?
0

$mongorow is an array. The function pref_replace does not accept an array as second parameter. You need to check the values of the array and check which one you need to use. Select that so called array value like $mongorow['value'].

If you are not introduced to arrays yet, don't hesitate to read this page.

1 Comment

First and second parameters in preg_replace can be both string or array.

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.