2016-02-05 49 views
0

我有一個數組是這樣的:PHP如何瀉×2陣列

array(2) { ["y"]=> string(1) "A" ["z"]=> string(3) "1,2" } 

我想打印出來的:

array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "107"},array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "2"} 

這裏是我的代碼:

$a = explode(",",$row['z']);

回答

1

假設你w螞蟻打印像...

array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "1"} 
array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "3"} 

...

那麼就應該工作。

$rowSplitted = array(); 
$zValues = explode(',', $row['z']) 
$yValue = $row['y'] 
foreach ($zValues as $zValue) { 
    $rowSplitted[] = array(
     'y' => $yValue, 
     'z' => $zValue, 
    ]; 
} 
0
$row = array("y"=>"A","z"=>"1,2"} 
$zArrayFromCSV = explode(",",$row['z']); 
$newArray = array(); 
foreach ($zArrayFromCSV as $valueZ) { 
    $newArray[] = array("y"=>$row['y'],"z"=>$valueZ); 
} 
var_dump($newArray); 
//array(2) 
//{ 
// [0]=>array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "1"} 
// [1]=>array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "2"} 
//}