Question: I need to restructure some array data then pass it as a json string to generate a Google line chart.

In the result array, I need to create rows of three elements.

1. the original element keys which are shared among all rows.
2. the values from the first row.
3. the values from the second row.

Input array:

```
[
    1 => [
        1 => 'c1',
        2 => 'c1',
        3 => 'c1',
        4 => 'c1',
        5 => 'c1'
    ],
    10 => [
        1 => 'c2',
        2 => 'c2',
        3 => 'c2',
        4 => 'c2',
        5 => 'c2'
    ]
]

```

Expected result is:

```
[[1,"c1","c2"],[2,"c1","c2"],[3,"c1","c2"],[4,"c1","c2"],[5,"c1","c2"]]

```

Answer: 
the below code will produce string as you wanted

```
$arr=Array
(
    "1" => Array
        (
            "1" => "c1",
            "2" => "c1",
            "3" => "c1",
            "4" => "c1",
            "5" => "c1",
            "6" => "c1",
            "7" => "c1",
            "8" => "c1",
            "9" => "c1",
            "10" => "c1",
            "11" => "c1",
            "12" => "c1"
        ),
    "10" => Array
        (
            "1" => "c2",
            "2" => "c2",
            "3" => "c2",
            "4" => "c2",
            "5" => "c2",
            "6" => "c2",
            "7" => "c2",
            "8" => "c2",
            "9" => "c2",
            "10" => "c2",
            "11" => "c2",
            "12" => "c2"
        )
);
$ii=0;
foreach($arr as $k =>$val)
{
$ii++;

 for ($i=1; $i <=count($val); $i++) { 
    if($ii == 2 ){
        $f[$i] .= ','.$val[$i] ."],";
    }else
    {
        $f[$i] = "['".$i."',". $val[$i] ;
    }
 }
}

$output = '';
foreach($f as $k => $val)
{
    $output .= $val;
}
echo $output;

```

***output***

```
['1',c1,c2],['2',c1,c2],['3',c1,c2],['4',c1,c2],['5',c1,c2],['6',c1,c2],['7',c1,c2],['8',c1,c2],['9',c1,c2],['10',c1,c2],['11',c1,c2],['12',c1,c2]

```
You merely need to include the keys from the first row in your [array transposition](https://stackoverflow.com/q/797251/2943403).

Also, it is crucial that you never manually generate json string by string concatenation (or any other manual method). The surest way to generate a valid json string is to call `json_encode()`.

Code: ([Demo](https://3v4l.org/kdgBX))

```
echo json_encode(
    array_map(
        null,
        array_keys(current($arr)),
        ...$arr
    )
);

```