将字符串转换为多维数组
我想 将此字符串:转换
$credit_packages_string = "300,0.25|1000,0.24|3000,0.22|4000,0.20|5000,0.18|6000,0.16|7000,0.14";
为此数组:
$credit_packages = array( array( 'credit_amount'=> 300,
'price_per_credit'=>0.25),
array( 'credit_amount'=> 1000,
'price_per_credit'=>0.24),
array( 'credit_amount'=> 3000,
'price_per_credit'=>0.22),
array( 'credit_amount'=> 4000,
'price_per_credit'=>0.20),
array( 'credit_amount'=> 5000,
'price_per_credit'=>0.18),
array( 'credit_amount'=> 6000,
'price_per_credit'=>0.16),
array( 'credit_amount'=> 7000,
'price_per_credit'=>0.14)
);
您将如何以最有效的方式做到这一点?
I'd like to
convert this string:
$credit_packages_string = "300,0.25|1000,0.24|3000,0.22|4000,0.20|5000,0.18|6000,0.16|7000,0.14";
into this array:
$credit_packages = array( array( 'credit_amount'=> 300,
'price_per_credit'=>0.25),
array( 'credit_amount'=> 1000,
'price_per_credit'=>0.24),
array( 'credit_amount'=> 3000,
'price_per_credit'=>0.22),
array( 'credit_amount'=> 4000,
'price_per_credit'=>0.20),
array( 'credit_amount'=> 5000,
'price_per_credit'=>0.18),
array( 'credit_amount'=> 6000,
'price_per_credit'=>0.16),
array( 'credit_amount'=> 7000,
'price_per_credit'=>0.14)
);
how would you do it in a most efficient way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
explode
来完成此操作;工作链接
You can do it using
explode
as;Working link
如果不使用正则表达式,您将需要一些循环,因此使用正则表达式可能是最好的
代码链接
Without using regular expression you'll need some loops so using a regex may be best
Link to code
这只会给你一个数字数组。如果您确实需要“credit_amount”和“price_per_credit”键,请添加以下内容:
This would only give you a numerical array. If you really want the 'credit_amount' and 'price_per_credit' keys, add the following: