2016-09-07 16 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();for($ i = 1; $ i $値として$アウト){ \t \t \t \t($ 0、$ <$ I; $ ++){ \t \t \tエコー$値[$ a]; \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()機能を使用してください。それは本当にクールです。
この問題を解決する方法は次のとおりです。あなたは私のコードで2次元配列に終わるでしょう。あなたは$fourthStep[0]$arr1にアクセスすることができますし、$は... $fourthStep[1]などでARR2

<?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

ありがとうございます。 –

関連する問題