Splitting array to use with jquery flot - php

I am building a jquery flot line chart with google analytics api data. I have the analytics working and it is giving me an array like so:
[3] => Array ( [0] => 20160922 [1] => 2217 [2] => 911 ) [4] => Array ( [0] => 20160923 [1] => 2047 [2] => 845 ) [5] => Array ( [0] => 20160924 [1] => 2152 [2] => 924 ) [6] => Array ( [0] => 20160925 [1] => 2502 [2] => 1028 ) [7] => Array ( [0] => 20160926 [1] => 2234 [2] => 877 ) [8] => Array ( [0] => 20160927 [1] => 2020 [2] => 755 ) [9] => Array ( [0] => 20160928 [1] => 1978 [2] => 793 ) [10] => Array ( [0] => 20160929 [1] => 2080 [2] => 867 ) [11] => Array ( [0] => 20160930 [1] => 1632 [2] => 747 ) [12] => Array ( [0] => 20161001 [1] => 1934 [2] => 913 ) [13] => Array ( [0] => 20161002 [1] => 2210 [2] => 1023 ) [14] => Array ( [0] => 20161003 [1] => 2068 [2] => 971 ) [15] => Array ( [0] => 20161004 [1] => 1738 [2] => 918 ) [16] => Array ( [0] => 20161005 [1] => 1787 [2] => 793 ) [17] => Array ( [0] => 20161006 [1] => 1694 [2] => 815 ) [18] => Array ( [0] => 20161007 [1] => 1583 [2] => 813 ) [19] => Array ( [0] => 20161008 [1] => 1906 [2] => 954 ) [20] => Array ( [0] => 20161009 [1] => 1936 [2] => 1047 ) [21] => Array ( [0] => 20161010 [1] => 2188 [2] => 1097 ) [22] => Array ( [0] => 20161011 [1] => 1892 [2] => 938 ) [23] => Array ( [0] => 20161012 [1] => 2036 [2] => 1022 ) [24] => Array ( [0] => 20161013 [1] => 1970 [2] => 915 ) [25] => Array ( [0] => 20161014 [1] => 2044 [2] => 1024 )
I cant seem to figure out how to get this separated out to use in a jquery.flot chart. The above array has a date followed by visits and pageviews that I want to put into a line-chart. All i need help with is the separating of the above array into one for visits and one for pageviews. The rest I can work out. Any help to get me pointed in the right direction will be great!
EDIT
I wanted to give some additional info as I cant seem to get the above array out of the results variable here is what I am using to process the google analytics api:
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
$optParams = array(
'dimensions' => 'ga:date'
);
return $analytics->data_ga->get(
'ga:' . $profileId,
'30daysAgo',
'today',
'ga:pageviews, ga:sessions',
$optParams
);
}
//$profile = $r['google_analytics'];
$profile = '69903642';
$results = getResults($analytics, $profile);
I believe the code will work that was given in the first answer but right now it is producing an empty array.

This code should work for you:
$pageViews = [];
$visits = [];
foreach($data as $key => $item) {
$pageViews[$key][$item[0]] = $item[2];
$visits[$key][$item[0]] = $item[1];
}
It will give you two separate arrays. Output:
array(
0 => array(
'20160922' => 911
)
)
array(
0 => array(
'20160922' => 2217
)
)
You can modify the code a little bit:
$pageViews = [];
$visits = [];
foreach($data as $key => $item) {
$pageViews[$item[0]] = $item[2];
$visits[$item[0]] = $item[1];
}
for output like this one:
array(
'20160922' => 2217,
......
)

Related

How do I use array_map recursively in PHP?

I am attempting to convert an associative array to a 2D array to allow me to export it to Google Sheets. I've figured out a simplistic solution that works as follows:
$headers = $data["resultSets"][0]["headers"];
$rowSet0 = $data["resultSets"][0]["rowSet"][0];
$rowSet1 = $data["resultSets"][0]["rowSet"][1];
$hackresults = array_map(null, $headers, $rowSet0, $rowSet1);
This produces the following:
(
[0] => Array
(
[0] => SEASON_ID
[1] => 22017
[2] => 22017
)
[1] => Array
(
[0] => Player_ID
[1] => 203954
[2] => 203954
)
[2] => Array
(
[0] => Game_ID
[1] => 0021701118
[2] => 0021701105
)
[3] => Array
(
[0] => GAME_DATE
[1] => MAR 28, 2018
[2] => MAR 26, 2018
)
[4] => Array
(
[0] => MATCHUP
[1] => PHI vs. NYK
[2] => PHI vs. DEN
)
[5] => Array
(
[0] => WL
[1] => W
[2] => W
)
[6] => Array
(
[0] => MIN
[1] => 9
[2] => 27
)
[7] => Array
(
[0] => FGM
[1] => 2
[2] => 6
)
[8] => Array
(
[0] => FGA
[1] => 6
[2] => 12
)
[9] => Array
(
[0] => FG_PCT
[1] => 0.333
[2] => 0.5
)
[10] => Array
(
[0] => FG3M
[1] => 0
[2] => 0
)
[11] => Array
(
[0] => FG3A
[1] => 1
[2] => 1
)
[12] => Array
(
[0] => FG3_PCT
[1] => 0
[2] => 0
)
[13] => Array
(
[0] => FTM
[1] => 1
[2] => 8
)
[14] => Array
(
[0] => FTA
[1] => 2
[2] => 10
)
[15] => Array
(
[0] => FT_PCT
[1] => 0.5
[2] => 0.8
)
[16] => Array
(
[0] => OREB
[1] => 2
[2] => 1
)
[17] => Array
(
[0] => DREB
[1] => 1
[2] => 12
)
[18] => Array
(
[0] => REB
[1] => 3
[2] => 13
)
[19] => Array
(
[0] => AST
[1] => 0
[2] => 2
)
[20] => Array
(
[0] => STL
[1] => 0
[2] => 1
)
[21] => Array
(
[0] => BLK
[1] => 0
[2] => 2
)
[22] => Array
(
[0] => TOV
[1] => 1
[2] => 4
)
[23] => Array
(
[0] => PF
[1] => 1
[2] => 5
)
[24] => Array
(
[0] => PTS
[1] => 5
[2] => 20
)
[25] => Array
(
[0] => PLUS_MINUS
[1] => 7
[2] => 20
)
[26] => Array
(
[0] => VIDEO_AVAILABLE
[1] => 1
[2] => 1
)
)
This is the output I'm looking for, but there are 27 "rowSet"s, and it seems there must be a recursive way of performing this task.
I've looked at a number of custom array_map_recursive style functions but haven't had any success. Apologies and thanks in advance, I am a terrible novice coder!
You can use argument unpacking.
With the ... operator, you can use all the elements under $data["resultSets"][0]["rowSet"] as additional arguments to array_map.
$headers = $data["resultSets"][0]["headers"];
$rowSets = $data["resultSets"][0]["rowSet"];
$results = array_map(null, $headers, ...$rowSets);
(This isn't recursion, but I think it does what you're trying to do.)

echo multidimensional array values into a readable format

I have a crazy array from google analytics API:
print_r($visits);
Produces the following:
Array ( [http_code] => 200 [kind] => analytics#gaData [id] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07 [query] => Array ( [start-date] => 2015-07-07 [end-date] => 2015-08-07 [ids] => ga:615743 [dimensions] => ga:date [metrics] => Array ( [0] => ga:visits ) [start-index] => 1 [max-results] => 1000 ) [itemsPerPage] => 1000 [totalResults] => 32 [selfLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07 [profileInfo] => Array ( [profileId] => 615743 [accountId] => 391435 [webPropertyId] => UA-391435-1 [internalWebPropertyId] => 642064 [profileName] => www.website.co.uk [tableId] => ga:615743 ) [containsSampledData] => [columnHeaders] => Array ( [0] => Array ( [name] => ga:date [columnType] => DIMENSION [dataType] => STRING ) [1] => Array ( [name] => ga:visits [columnType] => METRIC [dataType] => INTEGER ) ) [totalsForAllResults] => Array ( [ga:visits] => 8250 ) [rows] => Array ( [0] => Array ( [0] => 20150707 [1] => 271 ) [1] => Array ( [0] => 20150708 [1] => 266 ) [2] => Array ( [0] => 20150709 [1] => 251 ) [3] => Array ( [0] => 20150710 [1] => 264 ) [4] => Array ( [0] => 20150711 [1] => 351 ) [5] => Array ( [0] => 20150712 [1] => 244 ) [6] => Array ( [0] => 20150713 [1] => 309 ) [7] => Array ( [0] => 20150714 [1] => 250 ) [8] => Array ( [0] => 20150715 [1] => 277 ) [9] => Array ( [0] => 20150716 [1] => 214 ) [10] => Array ( [0] => 20150717 [1] => 215 ) [11] => Array ( [0] => 20150718 [1] => 167 ) [12] => Array ( [0] => 20150719 [1] => 228 ) [13] => Array ( [0] => 20150720 [1] => 290 ) [14] => Array ( [0] => 20150721 [1] => 236 ) [15] => Array ( [0] => 20150722 [1] => 245 ) [16] => Array ( [0] => 20150723 [1] => 267 ) [17] => Array ( [0] => 20150724 [1] => 307 ) [18] => Array ( [0] => 20150725 [1] => 271 ) [19] => Array ( [0] => 20150726 [1] => 226 ) [20] => Array ( [0] => 20150727 [1] => 319 ) [21] => Array ( [0] => 20150728 [1] => 299 ) [22] => Array ( [0] => 20150729 [1] => 263 ) [23] => Array ( [0] => 20150730 [1] => 242 ) [24] => Array ( [0] => 20150731 [1] => 233 ) [25] => Array ( [0] => 20150801 [1] => 165 ) [26] => Array ( [0] => 20150802 [1] => 170 ) [27] => Array ( [0] => 20150803 [1] => 349 ) [28] => Array ( [0] => 20150804 [1] => 410 ) [29] => Array ( [0] => 20150805 [1] => 282 ) [30] => Array ( [0] => 20150806 [1] => 256 ) [31] => Array ( [0] => 20150807 [1] => 113 ) ) )
If I replace print_r($visits); with
foreach ($visits as $key => $val) {
echo $val;
}
I get the following which is more readable:
200analytics#gaDatahttps://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07Array100032https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07ArrayArrayArrayArray
My question is, how do I access the Arrays within this Array?
I'd ideally like to print out the entire $visits array in something readable.
If you want this for debugging then output like this:
echo '<pre>';
print_r($visits);
echo '</pre>';
Other ways use array_walk_recursive()
You can use array_walk_recursive() to loop through each value of your array, e.g.
array_walk_recursive($visits, function($v, $k){
echo $v . "<br>";
});

How to change this kind of array arrangement into like this

I am getting data from Google Analytics API. The GA API returns these data:
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
)
)
I want that array to form like this:
$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...
);
So basically, all the 00 of bing,google, and yahoo is being grouped by on a single array, then the other 01,02,03,etc.. are to be grouped by on a single array again.
So if you noticed that we have a zero on this array array('01',0,7,0), it because bing doesn't have a 01 value in the array, and yahoo also doesn't have a 01 value in the array, except for google which has a 01 that has a value of 7.
Any help how to transform this kind of array to the one I posted.
Your help will be greatly appreciated! Thanks!
<?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);
?>
I managed to answer it on my own, not sure if this is efficient but it works! :)
//$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>";
Thanks again StackOverflow! :)
How about this:
<?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>';
And if someday you decide to add another search engine to the list all you'll have to do will be to add it to the $searchEngines array.

Function token_get_all not showing any tokens

I've got this simple code to test the output of token_get_all...
$arr = token_get_all("<?php $array=array(1,2,3); foreach($array as $key => $value) print($value); ?>");
print("<pre>");
print_r($arr);
print("</pre>");
But what ends up being displayed is this:
Array
(
[0] => Array
(
[0] => 372
[1] => 1
)
[1] => =
[2] => Array
(
[0] => 362
[1] => array
[2] => 1
)
[3] => (
[4] => Array
(
[0] => 305
[1] => 1
[2] => 1
)
[5] => ,
[6] => Array
(
[0] => 305
[1] => 2
[2] => 1
)
[7] => ,
[8] => Array
(
[0] => 305
[1] => 3
[2] => 1
)
[9] => )
[10] => ;
[11] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[12] => Array
(
[0] => 322
[1] => foreach
[2] => 1
)
[13] => (
[14] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[15] => Array
(
[0] => 326
[1] => as
[2] => 1
)
[16] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[17] => Array
(
[0] => 360
[1] => =>
[2] => 1
)
[18] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[19] => )
[20] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[21] => Array
(
[0] => 266
[1] => print
[2] => 1
)
[22] => (
[23] => )
[24] => ;
[25] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[26] => Array
(
[0] => 374
[1] => ?>
[2] => 1
)
)
From everything I've read about token_get_all, I'd expect the [0] key of these arrays to be the token names. What's going on with my code/server that I'm getting this instead?
I've also tried doing:
$arr = token_get_all(file_get_contents('someOtherValidPHPFile.php'));
And I get the same kind of result.
I'm using PHP version 5.4.19
Yes the token type is on index 0.
This is just a numeric value which identifies the token type. You can then compare them against the following list of token types: List of Parser Tokens
You can get the token name by using the token_name() function.
Tokens are defined as constants. E.g. the constant is named T_ARRAY and its value is 362. You can compare the tokens to that constant:
if ($token[0] == T_ARRAY) ...
If you want to get the readable name, use token_name.

determine average value from an array column

consider below array:-
Array
(
[0] => Array
(
[0] => 99895
[1] => 35378
[2] => 0.01
)
[1] => Array
(
[0] => 99895
[1] => 813
[2] => -0.97
)
[2] => Array
(
[0] => 99895
[1] => 771
[2] => 0.29
)
[3] => Array
(
[0] => 442
[1] => 833
[2] => -1.06
)
[4] => Array
(
[0] => 442
[1] => 485
[2] => -0.61
)
[5] => Array
(
[0] => 442
[1] => 367
[2] => -0.14
)
[6] => Array
(
[0] => 442
[1] => 478
[2] => 0.77
)
[7] => Array
(
[0] => 442
[1] => 947
[2] => -0.07
)
[8] => Array
(
[0] => 7977
[1] => 987
[2] => 0.76
)
[9] => Array
(
[0] => 7977
[1] => 819
[2] => 0.37
)
[10] => Array
(
[0] => 7977
[1] => 819
[2] => 0.36
)
[11] => Array
(
[0] => 7977
[1] => 653
[2] => 1.16
)
[12] => Array
(
[0] => 7977
[1] => 1653
[2] => 1.15
)
)
from the above array how will I determine the below array?
array
(
99895 => -0.223
442 => -0.22
7977 => 0.76
)
Actually I need the average value of column 3 in respect of column 1.
First collect all the column 3 elements into an array keyed off column 1:
$arrays = array();
foreach ($input as $vals) {
$key = $vals[0];
$val = $vals[2];
if (isset($arrays[$key])) {
$arrays[$key][] = $val;
} else {
$arrays[$key] = array($val);
}
}
Now go through all of them, calculating the averages:
foreach ($arrays as &$array) {
$array = array_sum($array)/count($array);
}

Categories