-1

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"]]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
kalyan
  • 171
  • 1
  • 11

5 Answers5

1

Two nested loops should do the trick in this case:

$input = /* your input array */;
$grouped = array();

foreach ($input as $elements) {
    foreach ($elements as $key => $element) {
        $grouped[$key][] = $element;
    }
}

This produces an array that looks like the following:

array(
    1 => array(c1, c2, ...),
    2 => array(c1, c2, ...),
    ...
);

This can the easily be converted to your desired output:

$output = "";
foreach ($grouped as $name => $values) {
    $output .= "['$name'";
    foreach ($values as $value)
        $output .= ", $value";
    $output .= "],\n";
}

Which will produce:

$output = "['1', c1, c2],\n['2', c1, c2]\n,...";
Majid Golshadi
  • 2,686
  • 2
  • 20
  • 29
Fabian Schuiki
  • 1,268
  • 7
  • 18
1

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]
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
1

For output as strings inside array:

<?php
$myArr = 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',
        ));
$result = array();
foreach($myArr as $subKey=>$subArr){
    foreach($subArr as $key=>$value){
        if(isset($result[$key])){
            $result[$key] .= $value;
        }else{
            $result[$key] =  $key.' , '.$value.' , ';
        }        
    }
}
var_dump($result);        
?>

output:

array (size=12)
  1 => string '1 , c1 , c2' (length=11)
  2 => string '2 , c1 , c2' (length=11)
  3 => string '3 , c1 , c2' (length=11)
  4 => string '4 , c1 , c2' (length=11)
  5 => string '5 , c1 , c2' (length=11)
  6 => string '6 , c1 , c2' (length=11)
  7 => string '7 , c1 , c2' (length=11)
  8 => string '8 , c1 , c2' (length=11)
  9 => string '9 , c1 , c2' (length=11)
  10 => string '10 , c1 , c2' (length=12)
  11 => string '11 , c1 , c2' (length=12)
  12 => string '12 , c1 , c2' (length=12)
Mohammad Alabed
  • 809
  • 6
  • 17
1

Here is an example that will work with any number of arrays:

$array = call_user_func_array( 'array_map', 
    array_merge( 
        array( function() { return func_get_args(); }, array_keys( $array[1] ) ), 
        $array 
    ) 
);

Since the expected output looks like JSON string i will use json_encode:

$output = json_encode( $array ); // [[1,"c1","c2"],[2,"c1","c2"],[3,"c1","c2"],[4, ...
$output = substr( json_encode( $array ), 1, -1 ); // [1,"c1","c2"],[2,"c1","c2"],[3,"c1","c2"],[4, ...

Or if you want the string without the quotes:

$output = array();
foreach( $array as $value ) {
    $output[] = '[' . implode( ',', $value ) . ']';
}
$output = implode( ',', $output ); // "[1,c1,c2],[2,c1,c2],[3,c1,c2],[4,c1,c2], ..."
Danijel
  • 12,408
  • 5
  • 38
  • 54
0

You merely need to include the keys from the first row in your array transposition.

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)

echo json_encode(
    array_map(
        null,
        array_keys(current($arr)),
        ...$arr
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136