如何将这种阵列排列改为这样

时间:2015-04-09 06:48:52

标签: php arrays google-analytics

我从Google AnalyticsAPI获取数据。 GA API会返回以下数据:

Array
(
    [0] => Array
        (
            [0] => 00
            [1] => bing
            [2] => 1
        )

    [1] => Array
        (
            [0] => 00
            [1] => google
            [2] => 12
        )

    [2] => Array
        (
            [0] => 00
            [1] => yahoo
            [2] => 1
        )

    [3] => Array
        (
            [0] => 01
            [1] => google
            [2] => 7
        )

    [4] => Array
        (
            [0] => 02
            [1] => google
            [2] => 5
        )

    [5] => Array
        (
            [0] => 03
            [1] => bing
            [2] => 1
        )

    [6] => Array
        (
            [0] => 03
            [1] => google
            [2] => 4
        )

    [7] => Array
        (
            [0] => 04
            [1] => google
            [2] => 7
        )

    [8] => Array
        (
            [0] => 05
            [1] => google
            [2] => 5
        )

    [9] => Array
        (
            [0] => 05
            [1] => yahoo
            [2] => 1
        )

    [10] => Array
        (
            [0] => 06
            [1] => bing
            [2] => 1
        )

    [11] => Array
        (
            [0] => 06
            [1] => google
            [2] => 2
        )

    [12] => Array
        (
            [0] => 07
            [1] => google
            [2] => 4
        )

    [13] => Array
        (
            [0] => 08
            [1] => bing
            [2] => 1
        )

    [14] => Array
        (
            [0] => 08
            [1] => google
            [2] => 8
        )

    [15] => Array
        (
            [0] => 09
            [1] => bing
            [2] => 4
        )

    [16] => Array
        (
            [0] => 09
            [1] => google
            [2] => 13
        )

    [17] => Array
        (
            [0] => 10
            [1] => bing
            [2] => 1
        )

    [18] => Array
        (
            [0] => 10
            [1] => google
            [2] => 19
        )

    [19] => Array
        (
            [0] => 10
            [1] => yahoo
            [2] => 1
        )

    [20] => Array
        (
            [0] => 11
            [1] => bing
            [2] => 1
        )

    [21] => Array
        (
            [0] => 11
            [1] => google
            [2] => 23
        )

    [22] => Array
        (
            [0] => 11
            [1] => yahoo
            [2] => 1
        )

    [23] => Array
        (
            [0] => 12
            [1] => bing
            [2] => 1
        )

    [24] => Array
        (
            [0] => 12
            [1] => google
            [2] => 18
        )

    [25] => Array
        (
            [0] => 13
            [1] => bing
            [2] => 1
        )

    [26] => Array
        (
            [0] => 13
            [1] => google
            [2] => 17
        )

    [27] => Array
        (
            [0] => 13
            [1] => yahoo
            [2] => 1
        )

    [28] => Array
        (
            [0] => 14
            [1] => bing
            [2] => 3
        )

    [29] => Array
        (
            [0] => 14
            [1] => google
            [2] => 30
        )

    [30] => Array
        (
            [0] => 14
            [1] => yahoo
            [2] => 2
        )

    [31] => Array
        (
            [0] => 15
            [1] => google
            [2] => 15
        )

    [32] => Array
        (
            [0] => 15
            [1] => yahoo
            [2] => 2
        )

    [33] => Array
        (
            [0] => 16
            [1] => bing
            [2] => 1
        )

    [34] => Array
        (
            [0] => 16
            [1] => google
            [2] => 22
        )

    [35] => Array
        (
            [0] => 16
            [1] => yahoo
            [2] => 1
        )

    [36] => Array
        (
            [0] => 17
            [1] => google
            [2] => 18
        )

    [37] => Array
        (
            [0] => 17
            [1] => yahoo
            [2] => 2
        )

    [38] => Array
        (
            [0] => 18
            [1] => google
            [2] => 15
        )

    [39] => Array
        (
            [0] => 19
            [1] => bing
            [2] => 1
        )

    [40] => Array
        (
            [0] => 19
            [1] => google
            [2] => 18
        )

    [41] => Array
        (
            [0] => 19
            [1] => yahoo
            [2] => 3
        )

    [42] => Array
        (
            [0] => 20
            [1] => google
            [2] => 15
        )

    [43] => Array
        (
            [0] => 21
            [1] => google
            [2] => 18
        )

    [44] => Array
        (
            [0] => 21
            [1] => yahoo
            [2] => 1
        )

    [45] => Array
        (
            [0] => 22
            [1] => bing
            [2] => 1
        )

    [46] => Array
        (
            [0] => 22
            [1] => google
            [2] => 21
        )

    [47] => Array
        (
            [0] => 23
            [1] => google
            [2] => 8
        )

    [48] => Array
        (
            [0] => 23
            [1] => yahoo
            [2] => 1
        )

)

我希望这个数组形成如下:

$example_data = array(
 array('00',1,12,1),//bing,google,yahoo
 array('01',0,7,0),
 array('02',0,5,0),
 array('03',1,4,0),
 array('04',0,7,0),
 array('05',0,5,1),
 array('06',1,2,0),
 array('07',0,7,0),
 array('08',1,8,0),
 array('09',0,13,0),
 array('10',1,19,1),
//should have more arrays in here, but I hope you got my point...
);

所以基本上,bing,google和yahoo的所有00都在一个数组上进行分组,然后是另一个010203, etc ..将再次在一个数组上进行分组。

因此,如果您注意到此数组array('01',0,7,0)上的零值为零,那么因为bing在数组中没有01值,而雅虎也没有数组中的01值,但Google的01值为7除外。

任何帮助如何将这种数组转换为我发布的数组。

非常感谢您的帮助!谢谢!

3 个答案:

答案 0 :(得分:1)

<?php
$array=json_decode('[["00","bing","1"],["00","google","12"],["00","yahoo","1"],["01","google","7"],["02","google","5"],["03","bing","1"],["03","google","4"],["04","google","7"],["05","google","5"],["05","yahoo","1"],["06","bing","1"],["06","google","2"],["07","google","4"],["08","bing","1"],["08","google","8"],["09","bing","4"],["09","google","13"],["10","bing","1"],["10","google","19"],["10","yahoo","1"],["11","bing","1"],["11","google","23"],["11","yahoo","1"],["12","bing","1"],["12","google","18"],["13","bing","1"],["13","google","17"],["13","yahoo","1"],["14","bing","3"],["14","google","30"],["14","yahoo","2"],["15","google","15"],["15","yahoo","2"],["16","bing","1"],["16","google","22"],["16","yahoo","1"],["17","google","18"],["17","yahoo","2"],["18","google","15"],["19","bing","1"],["19","google","18"],["19","yahoo","3"],["20","google","15"],["21","google","18"],["21","yahoo","1"],["22","bing","1"],["22","google","21"],["23","google","8"],["23","yahoo","1"]]');
$temp=array();
$temp1=array();
foreach($array as $arr){
    if(array_search($arr[0], $temp)===FALSE)
        $temp[]=$arr[0];
}
foreach($temp as $t){
    $bing=0;
    $google=0;
    $yahoo=0;
    foreach($array as $arr){
        if($t==$arr[0]){
            if($arr[1]=='bing'){
                $bing=$arr[2];
            }else if($arr[1]=='google'){
                $google=$arr[2];
            }else if($arr[1]=='yahoo'){
                $yahoo=$arr[2];
            }
        }
    }
    $temp1[]=array($t,$bing,$google,$yahoo);
}
var_dump($temp1);
?>

答案 1 :(得分:0)

这个怎么样:

<?php

$initial = [
    [ '00', 'bing', 1 ],
    [ '00', 'google', 12 ],
    [ '00', 'yahoo', 1 ],
    [ '01', 'google', 7 ],
    [ '02', 'google', 5 ],
    [ '03', 'bing', 1 ],
    [ '03', 'google', 4 ],
];

$searchEngines = ['bing', 'google', 'yahoo' ];

$temp = [];
foreach ( $initial as $data )
{
    // Create array of the type $data['00']['google'] = 12
    $temp[ $data[0] ][ $data[1] ] = $data[2];
}

$final = [];
// Now go over the newly created array
foreach( $temp as $code => $entry )
{
    // Initialize a new array holding initially that $code ( I don't know if $code is correct term though )
    $temp2 = [ $code ];
    // And now for each of the defined search engines
    foreach( $searchEngines as $se )
    {
        // Check if we have a value set
        if (isset( $entry[$se] ) )
        {
            // If we do - use it
            $temp2[] = $entry[$se];
        }
        else
        {
            // Otherwise use 0
            $temp2[] = 0;
        }
    }
    // Set it to the final resulting array
    $final[] = $temp2;
}

echo '<pre>';print_r( $final );echo '</pre>';

如果有一天你决定在列表中添加另一个搜索引擎,那么你只需要将它添加到$searchEngines数组中。

答案 2 :(得分:0)

我设法自己回答,不确定这是否有效但是有效! :)

//$new variable is the array that I the google analytics api returned
foreach($new as $key => $value){
    if($value[1] == 'bing')
        $combine['bing'][$value[0]] = $value[2];
    if($value[1] == 'google')
        $combine['google'][$value[0]] = $value[2];
    if($value[1] == 'yahoo')
        $combine['yahoo'][$value[0]] = $value[2];
}
$example_data = array();
for($i=0;$i<=23;$i++){
    $tempI = $i;
    if($i < 10)
        $tempI = "0".$i;
    $bing = 0;
    $google = 0;
    $yahoo = 0;
    if(isset($combine['bing'][$tempI])){
        $bing = $combine['bing'][$tempI];
    }
    if(isset($combine['google'][$tempI])){
        $google = $combine['google'][$tempI];
    }
    if(isset($combine['yahoo'][$tempI])){
        $yahoo = $combine['yahoo'][$tempI];
    }
    //time,bing,google,yahoo
    $example_data[$i][0] = $tempI;
    $example_data[$i][1] = $bing;
    $example_data[$i][2] = $google;
    $example_data[$i][3] = $yahoo;
}
echo "<pre>";
print_r($example_data);
echo "</pre>";

再次感谢StackOverflow! :)

相关问题