php数组string格式化,如何在PHP中从字符串格式化数组?

使用explode()函数!这个真的很酷。

以下是我将如何解决这个问题。你将以我的代码结束一个2d数组。您可以访问$arr1与$fourthStep[0]和$ ARR2与$fourthStep[1]等等

$str = "2,2;2;,1;1;,07-09-2016;07-09-2016;,08-09-2016;10-09-2016;,1;3;,100.00;450.00;";

$fourthStep = array();

//First, let's split that string up into something a little more.. readable.

$firstStep = explode(",",$str);

//$firstStep[0] contains our count for the total array count.

foreach($firstStep as $secondStep){ //Our second step is to loop through the newly created array which splits each section of your array

if ($secondStep != $firstStep[0]){ //skip the first part, as that is only telling us of array count

$thirdStep = explode(";",$secondStep); //third step is to get each data part of each section. The count of this array should be 'firstStep[0]-1'

for($i = 0; $i

//Now we want to assign the values into a 2D array

$fourthStep[$i][count($fourthStep[$i])] = $thirdStep[$i];

}

}

}

var_dump($fourthStep);

?>

结果:

array(2) { [0]=> array(6) { [0]=> string(1) "2" [1]=> string(1) "1" [2]=> string(10) "07-09-2016" [3]=> string(10) "08-09-2016" [4]=> string(1) "1" [5]=> string(6) "100.00" } [1]=> array(6) { [0]=> string(1) "2" [1]=> string(1) "1" [2]=> string(10) "07-09-2016" [3]=> string(10) "10-09-2016" [4]=> string(1) "3" [5]=> string(6) "450.00" } }

只是为了进一步的说明,你没有在第一部分所需要的“2”的字符串来计算出需要分割成多少个数组,因为它们使用2个不同的分离器,所以你可以很容易地解决它。保存像8位的空间或事端'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值