Sometimes i use things like that in pure php
$Subject = 'this key1, that key2';
$Replaces = ['key1'=>'value1','key2'=>'value2'];
$Return = str_replace(array_keys($Replaces), array_values($Replaces), $Subject);
// should return 'this value1, that value2
In Laravel collection that doesn't work, unless you convert the collection toArray();
so str_replace($Replaces->keys(), $Replaces->values(), $subject); // doesn't work
and str_replace(array_keys($Replaces->toArray(),...); // works
So, is it possible to achieve that by using collection?
I have no reason whatsoever to use collection, i just had that question in mind and thought to look it up, no i'm not stuck here! and i know that it's gonna be slower to use collection than to user pure php, but it's just something to scratch head with!
and thanks!
So, is it possible to achieve that by using collection?
Collection alone, no. The closest you'll see to this is the preg_replace_array()
and Str::replaceArray()
helpers, but those helpers replaces one key at a time and Illuminate\Support\Collection
does not call those functions anywhere, neither a str_replace
.