2015-12-27 61 views
-4

我有一個字符串,它看起來像:如何爆炸這個數組並顯示給調用變量?

$string = "12,32,23,32-23X.12,32,23,32-20X"; 

我怎樣才能把這個字符串轉換像一個數組:

Array ( 
    [0] => Array ( 
       [0] => Array ( 
          [0] => 12 
          [1] => 32 
          [2] => 23 
          [3] => 32 
         ) 
       [1] => 23X 
      ) 

    [1] => Array ( 
       [0] => Array ( 
          [0] => 11 
          [1] => 31 
          [2] => 21 
          [3] => 31 
         ) 
       [1] => 20X 
      ) 
) 

和結果將是:

12:23X 
32:23X 
23:23X 
32:23X 
11:20X 
31:20X 
21:20X 
31:20X 
+4

你嘗試過什麼嗎?這不是發佈作業的地方! –

+0

你不必像你給 – Prakash

+0

從數據的任何其中[0] =>數組( [0] => 11 [1] => 31 [2] => 21 [3] => 31 )來到 –

回答

1
<?php 
$string = "12,32,23,32-23X.12,32,23,32-20X"; 
$parts=explode(".",$string); 
foreach($parts as $part){ 
    $innerpart = explode("-",$part); 
    $nums = explode(",",$innerpart[0]); 
    foreach($nums as $num){ 
     echo $num.":".$innerpart[1]; 
    } 
} 
?> 

您必須在嵌套循環中使用三次explode()

+0

謝謝你的回答prakash,你很親切 –