Kingsley
Kingsley

Reputation: 193

Notice: array to string conversion in preg_replace php

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);

Upvotes: 2

Views: 2352

Answers (3)

Ruben
Ruben

Reputation: 343

$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.

Upvotes: 0

Flosi
Flosi

Reputation: 671

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.

Upvotes: 0

Moeed Farooqui
Moeed Farooqui

Reputation: 3622

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']);

Upvotes: 3

Related Questions