2016-09-07 48 views
1

我想如果字符串中的第一个数字是2,输出将是2数组。如何像字符串中的每个数组一样爆炸。如何在PHP中从字符串格式化数组?

我的代码

<?php 

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


$data = explode(',',$str); 
$out = array(); 
for($i=1;$i < count($data)-1;$i++){ 

    $out[]= explode(';',$data[$i]); 

} 

$i = $out[0][0]; 


foreach ($out as $key => $value) { 


for($a=0;$a < $i; $a++){ 

    echo $value[$a]. "<br/>"; 
} 

} 

?> 

我得到的结果221107-09-201607-09-201608-09-201610-09-201613 但我想这种格式

<?php 

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

//format will be split by semicomma ; 
$arr1 = Array('2','1','07-09-2016','08-09-2016','1','100.00'); 
$arr2 = Array('2','1','07-09-2016','10-09-2016','3','450.00'); 

?> 
+0

使用'explode()'和'array_map()'...? https://eval.in/637129 –

+0

我使用这个,但它不行$ data = explode(',',$ str); $ out = array(); ($; $; $ data)-1; $ i ++){ \t \t $ out [] = explode(';',$ data [$ i]); \t } $ i = $ out [0] [0]; 的foreach($出为$密钥=> $值){ \t \t \t \t为($ a = 0; $一个<$ I; $一个++){ \t \t \t回波$值[$一个]; \t} \t \t } –

+0

请编辑您的帖子以包含您对问题的任何其他信息。避免在评论中添加这些内容,因为它们难以阅读并且可以更容易地删除。您帖子的修改按钮位于帖子标签下方。 – Rizier123

回答

2

的php函数array_column会在这里派上用场。这里是简短的代码示例,应该输出您正在查找的内容。

<?php 
//Your original input 
$str =  "2,2;2;,1;1;,07-09-2016;07-09-2016;,08-09-2016;10-09-2016;,1;3;,100.00;450.00"; 

//explode the array into its sub-arrays 
$arrs = explode(",", $str); 

//remove the first element that sets how many elements are in each array 
$numArrs = array_shift($arrs); 

//convert strings into those wanted sub-arrays 
array_walk($arrs, function(&$val, $key) { $val = explode(';',$val); }); 

//make the answer we need 
$ans = array(); 
for($i=0; $i<$numArrs; $i++) { 
    //array_column does all the work that we want, making life easy 
    $ans[] = array_column($arrs, $i); 
} 

var_dump($ans); 

这个过程确实承担字符串格式正确,我们正在寻找 - 这将失败可怕,如果事实并非如此。

+0

非常感谢。我需要。 –

1

使用explode()函数!这个真的很酷。
以下是我将如何解决这个问题。你将以我的代码结束一个2d数组。您可以访问$arr1$fourthStep[0]和$ ARR2与$fourthStep[1]等等

<?php 
$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<$firstStep[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

谢谢指出。 –