0

I have an array which is like:

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

I want to print them as:

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

Here is my code:

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

0

2 Answers 2

1

Assuming that you want to print it like...

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

...

then that should work.

$rowSplitted = array();
$zValues = explode(',', $row['z'])
$yValue = $row['y']
foreach ($zValues as $zValue) {
    $rowSplitted[] = array(
        'y' => $yValue,
        'z' => $zValue,
    ];
}
Sign up to request clarification or add additional context in comments.

Comments

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"}
//}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.