dongpan1365 2019-07-23 13:03
浏览 224
已采纳

什么是str_replace的等价物(array_keys([]),array_values([]),$ subject); 在Laravel Collection

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!

  • 写回答

1条回答 默认 最新

  • drpjdfj618393 2019-08-02 05:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化