2016-09-07 45 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

謝謝指出。 –