如何爆炸这个数组并显示给调用变量?

时间:2015-12-27 07:35:28

标签: php arrays

我有一个看起来像的字符串:

$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

1 个答案:

答案 0 :(得分: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()三次。