将PHP字符串中两个字符之间的数字相乘
Posted
技术标签:
【中文标题】将PHP字符串中两个字符之间的数字相乘【英文标题】:Multiply the number between two characters in PHP string 【发布时间】:2020-08-16 17:15:16 【问题描述】:请查看最后的 EDIT。
我正在从外部 URL 解析无效的 json 数据,并且我想修改特定键的值。 我想将值与特定键(分)的值相乘。
如果你仔细观察,模式会重复,它以“vk”开头。
如何将“cent”的值与常数 (0.887) 相乘。
我从外部 URL 解析的示例数据如下所示:
"0":"vk":"cent":21,"bank":1453,"ok":"cent":4,"bank":2581,"wa":"cent":4.5,"bank":959,"vi":"cent":4,"bank":0,"tg":"cent":4.5,"bank":0,"wb":"cent":14,"bank":6733,"go":"cent":5.5,"bank":149,"av":"cent":2.5,"bank":2139,"av_1":"cent":59,"bank":0,"fb":"cent":3,"bank":84,"tw":"cent":2,"bank":3848,"ot_1":"cent":59,"bank":0,"ub":"cent":1,"bank":4042,"gt":"cent":1,"bank":6835,"ig":"cent":5,"bank":3094,"ym":"cent":2,"bank":16,"ym_1":"cent":59,"bank":0,"ma":"cent":1,"bank":5282,"mm":"cent":2,"bank":0,"uk":"cent":2,"bank":7097,"mb":"cent":2.5,"bank":2694,"we":"cent":1,"bank":7010,"bd":"cent":13,"bank":0,"dt":"cent":2,"bank":5592,"ya":"cent":2,"bank":3811,"ya_1":"cent":59,"bank":0,"mt":"cent":4,"bank":6468,"oi":"cent":1.25,"bank":1200,"fd":"cent":1,"bank":5505,"zz":"cent":2,"bank":7097,"kt":"cent":1.5,"bank":6585,"pm":"cent":1.5,"bank":983,"tn":"cent":1,"bank":7112,"qq":"cent":2,"bank":5796,"mg":"cent":1,"bank":2260,"yl":"cent":1,"bank":5950,"po":"cent":2,"bank":5645,"nv":"cent":2,"bank":3378,"nf":"cent":2,"bank":6210,"im":"cent":1.5,"bank":217,"ds":"cent":1.25,"bank":5796,"vv":"cent":1,"bank":6685,"lf":"cent":1.5,"bank":6582,"gp":"cent":1.5,"bank":6682,"am":"cent":1,"bank":6674,"tc":"cent":1,"bank":6683,"dp":"cent":1,"bank":6324,"yf":"cent":2,"bank":6587,"op":"cent":2,"bank":6331,"fx":"cent":2,"bank":6525,"qr":"cent":2,"bank":6664,"yk":"cent":2,"bank":6650,"ls":"cent":2,"bank":6668,"bl":"cent":2,"bank":6025,"mu":"cent":2,"bank":6627,"fu":"cent":2,"bank":6666,"sg":"cent":2,"bank":6387,"uu":"cent":2,"bank":6678,"ua":"cent":2,"bank":6357,"ab":"cent":2,"bank":6127,"iv":"cent":2,"bank":6685,"fy":"cent":2,"bank":6351,"ce":"cent":2,"bank":6279,"hm":"cent":2,"bank":6317,"tx":"cent":2,"bank":6669,"pl":"cent":2,"bank":6682,"ip":"cent":2,"bank":6314,"hw":"cent":2,"bank":6085,"de":"cent":1,"bank":6548,"jc":"cent":2,"bank":6683,"rl":"cent":2,"bank":6594,"df":"cent":2,"bank":6353,"ui":"cent":1.5,"bank":6633,"up":"cent":2,"bank":6670,"kf":"cent":1,"bank":6685,"za":"cent":2,"bank":6683,"da":"cent":3,"bank":5469,"sq":"cent":2,"bank":6685,"li":"cent":2,"bank":6486,"rd":"cent":2,"bank":6650,"qb":"cent":2,"bank":6685,"hz":"cent":2,"bank":6684,"ia":"cent":2,"bank":6685,"ew":"cent":1.5,"bank":6681,"ae":"cent":1,"bank":6654,"gb":"cent":1,"bank":6681,"cy":"cent":3,"bank":5642,"qm":"cent":2.5,"bank":6684,"yb":"cent":2.5,"bank":6761,"ot":"cent":2.5,"bank":0"1":"vk":"cent":21,"bank":6,"ok":"cent":5,"bank":1594,"wa":"cent":8,"bank":0,"vi":"cent":7,"bank":3,"tg":"cent":8,"bank":0,"wb":"cent":15,"bank":952,"go":"cent":10,"bank":2,"av":"cent":4,"bank":1517,"fb":"cent":3.5,"bank":3,"tw":"cent":2,"bank":453,"ub":"cent":2,"bank":610,"gt":"cent":1,"bank":1511,"sn":"cent":40,"bank":693,"ig":"cent":6,"bank":0,"ss":"cent":1,"bank":1516,"ym":"cent":2,"bank":1434,"ma":"cent":1,"bank":1140,"mm":"cent":2,"bank":0,"uk":"cent":2,"bank":1447,"me":"cent":1,"bank":1516,"mb":"cent":2.5,"bank":262,"we":"cent":1,"bank":1517,"bd":"cent":1,"bank":1509,"kp":"cent":2,"bank":1466,"dt":"cent":1,"bank":1515,"ya":"cent":1,"bank":558,"mt":"cent":4,"bank":1923,"oi":"cent":2,"bank":474,"fd":"cent":2,"bank":1448,"zz":"cent":2,"bank":1509,"kt":"cent":2,"bank":580,"pm":"cent":1.5,"bank":1,"tn":"cent":1,"bank":1499,"qq":"cent":2,"bank":1022,"yl":"cent":1,"bank":1459,"po":"cent":6,"bank":1478,"nv":"cent":6,"bank":611,"nf":"cent":6,"bank":1334,"im":"cent":2,"bank":188,"ds":"cent":1.5,"bank":187,"vv":"cent":2,"bank":1453,"ji":"cent":2,"bank":998,"lf":"cent":2,"bank":1470,"hu":"cent":6,"bank":854,"wg":"cent":4,"bank":991,"rz":"cent":2,"bank":1408,"la":"cent":6,"bank":1477,"zh":"cent":1,"bank":1430,"gp":"cent":1.5,"bank":1458,"ls":"cent":6,"bank":1478,"zy":"cent":6,"bank":1476,"tx":"cent":6,"bank":1260,"cm":"cent":6,"bank":1384,"hw":"cent":6,"bank":853,"ri":"cent":9,"bank":1476,"za":"cent":2,"bank":1453,"gl":"cent":3,"bank":1366,"dh":"cent":4,"bank":1409,"ot":"cent":6,"bank":519
我试过的代码:
$json = get_content('tld.xyz/json.php?bank=prices');
$data = json_decode($json, true);
foreach ($decoded_json['cent'] as &$value)
if ($value >= 0) $value = $value * 0.887; $result[]=$value;
$fin = json_encode($data);
echo $fin;
编辑:我认为它不是 json,可以通过使用正则表达式来解决。我正在考虑找到 : 和 , 之间的字符串,以及如何将此数字与我的常数相乘。
感谢任何帮助。我不是网络开发人员,但我有一个小项目,我想自己解决这个问题,而不是雇人。
谢谢。
【问题讨论】:
我能够提取和解码 json,但我需要搜索键的值并进行乘法运算。我已经看到很多类似的问题,但我的不同。 我尝试了这段代码,因为它看起来和我的问题相似,但我得到的结果仍然是一样的。 ***.com/a/38880531/9488870 我使用的代码: $json = get_content('tld.xyz/json.json'); $data = json_decode($json, true); foreach ($decoded_json['cost'] as &$value) if ($value >= 0) $value = $value * 2; $结果[]=$值; $fin = json_encode($data);回声 $fin;/cent":(\d*),/
(flags gm) 这是您需要的正则表达式,以便将您的号码捕获到组 $1 - 然后使用 preg_replace
。抱歉在手机上,所以我不能写完整的答案(我可以,但是太痛苦了,很难输入正则表达式!)。
您的 get_content 多次也是您的 JSON 无效的原因。您可能最好在每次调用 JSON 时将其转换为数组并合并数组(然后您可以在 PHP 中操作数据),然后恢复为 JSON。
嗨,mdhz - 我还在学习 php。不过有一个问题:bestprogrammerintheworld 的解决方案终于做到了——它对你有用吗?很高兴收到您的来信
【参考方案1】:
<?php
$json = '"0":"vk":"cent":21,"bank":1453,"ok":"cent":4,"bank":2581,"wa":"cent":4.5,"bank":959,"vi":"cent":4,"bank":0,"tg":"cent":4.5,"bank":0,"wb":"cent":14,"bank":6733,"go":"cent":5.5,"bank":149,"av":"cent":2.5,"bank":2139,"av_1":"cent":59,"bank":0,"fb":"cent":3,"bank":84,"tw":"cent":2,"bank":3848,"ot_1":"cent":59,"bank":0,"ub":"cent":1,"bank":4042,"gt":"cent":1,"bank":6835,"ig":"cent":5,"bank":3094,"ym":"cent":2,"bank":16,"ym_1":"cent":59,"bank":0,"ma":"cent":1,"bank":5282,"mm":"cent":2,"bank":0,"uk":"cent":2,"bank":7097,"mb":"cent":2.5,"bank":2694,"we":"cent":1,"bank":7010,"bd":"cent":13,"bank":0,"dt":"cent":2,"bank":5592,"ya":"cent":2,"bank":3811,"ya_1":"cent":59,"bank":0,"mt":"cent":4,"bank":6468,"oi":"cent":1.25,"bank":1200,"fd":"cent":1,"bank":5505,"zz":"cent":2,"bank":7097,"kt":"cent":1.5,"bank":6585,"pm":"cent":1.5,"bank":983,"tn":"cent":1,"bank":7112,"qq":"cent":2,"bank":5796,"mg":"cent":1,"bank":2260,"yl":"cent":1,"bank":5950,"po":"cent":2,"bank":5645,"nv":"cent":2,"bank":3378,"nf":"cent":2,"bank":6210,"im":"cent":1.5,"bank":217,"ds":"cent":1.25,"bank":5796,"vv":"cent":1,"bank":6685,"lf":"cent":1.5,"bank":6582,"gp":"cent":1.5,"bank":6682,"am":"cent":1,"bank":6674,"tc":"cent":1,"bank":6683,"dp":"cent":1,"bank":6324,"yf":"cent":2,"bank":6587,"op":"cent":2,"bank":6331,"fx":"cent":2,"bank":6525,"qr":"cent":2,"bank":6664,"yk":"cent":2,"bank":6650,"ls":"cent":2,"bank":6668,"bl":"cent":2,"bank":6025,"mu":"cent":2,"bank":6627,"fu":"cent":2,"bank":6666,"sg":"cent":2,"bank":6387,"uu":"cent":2,"bank":6678,"ua":"cent":2,"bank":6357,"ab":"cent":2,"bank":6127,"iv":"cent":2,"bank":6685,"fy":"cent":2,"bank":6351,"ce":"cent":2,"bank":6279,"hm":"cent":2,"bank":6317,"tx":"cent":2,"bank":6669,"pl":"cent":2,"bank":6682,"ip":"cent":2,"bank":6314,"hw":"cent":2,"bank":6085,"de":"cent":1,"bank":6548,"jc":"cent":2,"bank":6683,"rl":"cent":2,"bank":6594,"df":"cent":2,"bank":6353,"ui":"cent":1.5,"bank":6633,"up":"cent":2,"bank":6670,"kf":"cent":1,"bank":6685,"za":"cent":2,"bank":6683,"da":"cent":3,"bank":5469,"sq":"cent":2,"bank":6685,"li":"cent":2,"bank":6486,"rd":"cent":2,"bank":6650,"qb":"cent":2,"bank":6685,"hz":"cent":2,"bank":6684,"ia":"cent":2,"bank":6685,"ew":"cent":1.5,"bank":6681,"ae":"cent":1,"bank":6654,"gb":"cent":1,"bank":6681,"cy":"cent":3,"bank":5642,"qm":"cent":2.5,"bank":6684,"yb":"cent":2.5,"bank":6761,"ot":"cent":2.5,"bank":0"1":"vk":"cent":21,"bank":6,"ok":"cent":5,"bank":1594,"wa":"cent":8,"bank":0,"vi":"cent":7,"bank":3,"tg":"cent":8,"bank":0,"wb":"cent":15,"bank":952,"go":"cent":10,"bank":2,"av":"cent":4,"bank":1517,"fb":"cent":3.5,"bank":3,"tw":"cent":2,"bank":453,"ub":"cent":2,"bank":610,"gt":"cent":1,"bank":1511,"sn":"cent":40,"bank":693,"ig":"cent":6,"bank":0,"ss":"cent":1,"bank":1516,"ym":"cent":2,"bank":1434,"ma":"cent":1,"bank":1140,"mm":"cent":2,"bank":0,"uk":"cent":2,"bank":1447,"me":"cent":1,"bank":1516,"mb":"cent":2.5,"bank":262,"we":"cent":1,"bank":1517,"bd":"cent":1,"bank":1509,"kp":"cent":2,"bank":1466,"dt":"cent":1,"bank":1515,"ya":"cent":1,"bank":558,"mt":"cent":4,"bank":1923,"oi":"cent":2,"bank":474,"fd":"cent":2,"bank":1448,"zz":"cent":2,"bank":1509,"kt":"cent":2,"bank":580,"pm":"cent":1.5,"bank":1,"tn":"cent":1,"bank":1499,"qq":"cent":2,"bank":1022,"yl":"cent":1,"bank":1459,"po":"cent":6,"bank":1478,"nv":"cent":6,"bank":611,"nf":"cent":6,"bank":1334,"im":"cent":2,"bank":188,"ds":"cent":1.5,"bank":187,"vv":"cent":2,"bank":1453,"ji":"cent":2,"bank":998,"lf":"cent":2,"bank":1470,"hu":"cent":6,"bank":854,"wg":"cent":4,"bank":991,"rz":"cent":2,"bank":1408,"la":"cent":6,"bank":1477,"zh":"cent":1,"bank":1430,"gp":"cent":1.5,"bank":1458,"ls":"cent":6,"bank":1478,"zy":"cent":6,"bank":1476,"tx":"cent":6,"bank":1260,"cm":"cent":6,"bank":1384,"hw":"cent":6,"bank":853,"ri":"cent":9,"bank":1476,"za":"cent":2,"bank":1453,"gl":"cent":3,"bank":1366,"dh":"cent":4,"bank":1409,"ot":"cent":6,"bank":519';
//JSON supplied is not valid, it misses [] at the beginning and the end
//and it also misses a comma between the keys which is solved by next two lines
$json = str_replace("",",", $json);
$valid_json = '[' . $json . ']';
$data = json_decode($valid_json, true);
//Copy the current data into new array $na
$na = array_slice($data,0,count($data));
//Change the copy of array $data with cents
foreach($data as $top_key=>$value)
$level_arr = $na[$top_key][$top_key];
foreach($level_arr as $key=>$item)
if ($na[$top_key][$top_key][$key]['cent']>0)
$na[$top_key][$top_key][$key]['cent'] = $data[$top_key][$top_key][$key]['cent'] * 0.087;
//JSON
echo json_encode($na);
更新1:
您可以删除 if ($na[$top_key][$top_key][$key]['cent']>0)
- 部分。
//Change the copy of array $data with cents
foreach($data as $top_key=>$value)
$level_arr = $na[$top_key][$top_key];
foreach($level_arr as $key=>$item)
$na[$top_key][$top_key][$key]['cent'] = $data[$top_key][$top_key][$key]['cent'] * 0.087;
//JSON
echo json_encode($na);
更新 2:
以上要求数组的层级为:
[0][0][whatever key]['cent]
[1][1][whatever key]['cent]
[2][2][whatever key]['cent]
如果您想要更多的灵活性而不是依赖***密钥和二级密钥是相同的([0][0], [1][1]
等...)
你可以这样做:(替换上面给出的代码)
//Change the copy of array $data with cents
foreach($data as $top_key=>$second_level)
foreach($second_level as $key2=>$value)
$level_arr = $na[$top_key][$key2];
foreach($level_arr as $key=>$item)
$na[$top_key][$key2][$key]['cent'] = (float)$data[$top_key][$key2][$key]['cent'] * 0.087;
更新3:
我错过了在 update2 中将 $top_key
更改为 $key2
(现在我将其更正为),但这里的代码几乎相同,但结果为基于实际数据(来自外部 API)的两位小数成本作为关键而不是美分。我也将实际常数改为 0.877 而不是 0.087。
foreach($data as $top_key=>$second_level)
foreach($second_level as $key2=>$level_arr)
foreach($level_arr as $key=>$item)
$na[$top_key][$key2][$key]['cost'] =
number_format(
(float)$data[$top_key][$key2][$key]['cost'] * 0.877,2
);
【讨论】:
这行得通,但是一些键值是整数类型并且它们没有改变,那些是字符串的值改变了。示例:array(3) [0]=> array(1) [7]=> array(18) ["vk"]=> array(2) ["cent"]=> int(10) ["bank"]=> int(0) ["ok"]=> array(2) ["cent"]=> int(8) ["bank"]=> int(0) ["wa "]=> array(2) ["cent"]=> int(18) ["bank"]=> int(88) ["vi"]=> array(2) ["cent"]= > int(12) ["bank"]=> int(4088) 这里不会失败,但你可以强制转换为浮动:$data[$top_key][$top_key][$key]['cent'] * 0.087 TO float()$data[$ top_key][$top_key][$key]['cent'] * 0.087。这样做有用吗? 我试过了,但没有成功。 $na[$top_key][$top_key][$key]['cent'] = number_format(floatval($data[$top_key][$top_key][$key]['cent']) * 0.877, 2);因为我想要两位小数。 告诉我一个确切的值不起作用(哪个键、子键等和值)? (在之前和之后告诉我),我也许可以为您提供进一步的帮助。 我必须承认我从一开始就设置了错误的常量,这让事情变得更加混乱。以上是关于将PHP字符串中两个字符之间的数字相乘的主要内容,如果未能解决你的问题,请参考以下文章