I’ve got a problem which I can’t seem to find a solution to. In a foreach loop
foreach( $results->result as $item )
the following code
$item->title = str_replace( " - Home", "", $item->title );
always throws an error
Catchable fatal error: Object of class stdClass could not be converted to string
I thought str_replace was able to handle arrays? What would I need to change in order to get this working?
Thanks for any advice!
It’s not an array, it’s an object, as the error says… did you get it with
json_decode()by any chance?However, the solution is simple – cast it to an array:
Another point about this is that if you are wanting to modify the data held in
$results->result, and not just a copy of it, you will need yourforeachto be:…and get item as a reference, not a copy…