从php数组中获取列格式的值

时间:2014-06-21 08:27:49

标签: php arrays linechart

我正在使用谷歌折线图。所以我想准备这里解释的数据。我有下面给出的php数组。我需要动态地将每个数组值分配在一个php变量中作为单个列(如下面的数组)。我怎样才能做到这一点?

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
        )
)

预期结果是

$output=”
['1',   c1,      c2],
['2',   c1,      c2],
.
.
.
['12',   c1,      c2],
“;

4 个答案:

答案 0 :(得分:1)

在这种情况下,两个嵌套循环应该可以解决问题:

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

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

这将生成一个如下所示的数组:

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

这可以很容易地转换为您想要的输出:

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

将产生:

$output = "['1', c1, c2],\n['2', c1, c2]\n,...";

答案 1 :(得分:1)

以下代码将生成您想要的字符串

$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;

<强> 输出

['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]

答案 2 :(得分:1)

输出为数组中的字符串:

<?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);        
?>

<强>输出:

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)

答案 3 :(得分:1)

以下示例适用于任意数量的数组:

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

由于预期输出看起来像JSON字符串,我将使用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, ...

或者如果你想要没有引号的字符串:

$output = array();
foreach( $array as $value ) {
    $output[] = '[' . implode( ',', $value ) . ']';
}
$output = implode( ',', $output ); // "[1,c1,c2],[2,c1,c2],[3,c1,c2],[4,c1,c2], ..."