duannao3819 2018-12-06 01:52
浏览 45
已采纳

将javascript array.map转换为php array_map

This is a javascript code that consists of hashmap array(keys:value).I created one function using map that returns values of entered keys.

var rule = 
{
"c": "d",
"a": "o",
"t": "g",
"h": "a",
"e": "n",
"n": "t"
}
function convert(str) {
return [...str].map(d => rule[d]).join('')
}
console.log(convert("cat"))
//prints dog

Now I want to convert above javascript code in php so that i can run it under php server. I created same array in php as

$rule = 
{
"c" => "d",
"a" => "o",
"t" => "g",
"h" => "a",
"e" => "n",
"n" => "t"
}

Using array_map,how to convert in php code in same way as done in javascript. Php syntax for array_map is

array_map(myfunction,array1,array2...)
  • 写回答

1条回答 默认 最新

  • dongyiba8082 2018-12-06 01:55
    关注

    Such function exists in php

    echo strtr('cat', $rule);
    

    demo

    EDIT:

    To do with array_map

    echo join('', array_map(function($x) use($rule) { return $rule[$x]; }, str_split('cat')));
    

    demo

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

报告相同问题?

悬赏问题

  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码