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 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效