将字符串转换为多维数组

发布于 2024-09-26 23:38:01 字数 770 浏览 1 评论 0原文

我想 将此字符串:转换

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九命猫 2024-10-03 23:38:01

您可以使用 explode 来完成此操作;

$result = array();
$credits = explode('|',$credit_packages_string);
foreach($credits as $credit) {
        $credit_part = explode(',',$credit);
        $result[] = array('credit_amount'    => $credit_part[0],
                          'price_per_credit' => $credit_part[1]);
}

工作链接

You can do it using explode as;

$result = array();
$credits = explode('|',$credit_packages_string);
foreach($credits as $credit) {
        $credit_part = explode(',',$credit);
        $result[] = array('credit_amount'    => $credit_part[0],
                          'price_per_credit' => $credit_part[1]);
}

Working link

屋顶上的小猫咪 2024-10-03 23:38:01

如果不使用正则表达式,您将需要一些循环,因此使用正则表达式可能是最好的

$credit_packages_string = "300,0.25|1000,0.24|3000,0.22|4000,0.20|5000,0.18|6000,0.16|7000,0.14";
preg_match_all( '~(?P<credit_amount>\d+),(?P<price_per_credit>[\d\.]+)~', $credit_packages_string, $matches, PREG_SET_ORDER );
print_r($matches);

代码链接

Without using regular expression you'll need some loops so using a regex may be best

$credit_packages_string = "300,0.25|1000,0.24|3000,0.22|4000,0.20|5000,0.18|6000,0.16|7000,0.14";
preg_match_all( '~(?P<credit_amount>\d+),(?P<price_per_credit>[\d\.]+)~', $credit_packages_string, $matches, PREG_SET_ORDER );
print_r($matches);

Link to code

○闲身 2024-10-03 23:38:01
$array = explode(',', explode('|', $credit_packages_string));

这只会给你一个数字数组。如果您确实需要“credit_amount”和“price_per_credit”键,请添加以下内容:

foreach($i as &$array) {
    $i['credit_amount'] = $i[0];
    $i['price_per_credit'] = $i[1];
}
$array = explode(',', explode('|', $credit_packages_string));

This would only give you a numerical array. If you really want the 'credit_amount' and 'price_per_credit' keys, add the following:

foreach($i as &$array) {
    $i['credit_amount'] = $i[0];
    $i['price_per_credit'] = $i[1];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文