How do I use array_map recursively in PHP? - 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.)

Related

Grouping php arrays and showing highest and lowest value in the array

I have an array that looks like this:
Array
(
[0] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 1
)
[1] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 3
)
[3] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 4
)
[4] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 5
)
[5] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 6
)
[6] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 7
)
[7] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 8
)
[8] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 9
)
[9] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 10
)
[10] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 11
)
[11] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 12
)
[12] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 20
)
[13] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 21
)
[14] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 22
)
[15] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 23
)
[16] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 24
)
[17] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 25
)
[18] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 26
)
[19] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 27
)
[20] => Array
(
[0] => 19.0001.2
[1] => 1
[2] => 33
)
[21] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 30
)
[22] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 35
)
[23] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 46
)
[24] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 47
)
[25] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 48
)
[26] => Array
(
[0] => 19.0001.2
[1] => 2
[2] => 50
)
[27] => Array
(
[0] => 19.0001.2
[1] => 3
[2] => 1
)
[28] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 40
)
[29] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 41
)
[30] => Array
(
[0] => 19.0001.3
[1] => 2
[2] => 42
)
[31] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 7
)
[32] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 8
)
[33] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 9
)
[34] => Array
(
[0] => 19.0001.3
[1] => 3
[2] => 13
)
[35] => Array
(
[0] => 19.0001.3
[1] => 4
[2] => 41
)
[36] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 2
)
[37] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 3
)
[38] => Array
(
[0] => 19.0001.4
[1] => 1
[2] => 4
)
[39] => Array
(
[0] => 19.0001.8
[1] => 4
[2] => 40
)
[40] => Array
(
[0] => 19.0009.8
[1] => 3
[2] => 8
)
)
I have a function that groups the last value in an array into a range of numbers, however it doesn't work with the above array. I have tried a number of different arrays with no success. The above array is generated by a mysql query result.
$getlocations = $db->query("
SELECT barcode
, test_num
, magnum
, sect
, slidenum
FROM fas_log
WHERE barcode LIKE '%$bcinv'
AND notes = '$notes'
AND removed = '$removed'
AND staffid= '$sid'
ORDER BY magnum, sect, slidenum
");
while ($getlocationsrow = $getlocations->fetch()) {
extract($getlocationsrow);
$magrange[]=array("$magnum", "$sect", "$slidenum");
}
function rangeslides($arr) {
$previous = null;
$result = array();
$consecutiveArray = array();
// Slice array by consecutive sequences
foreach($arr as $number) {
if ($number == $previous + 1) {
$consecutiveArray[] = $number;
} else {
$result[] = $consecutiveArray;
$consecutiveArray = array($number);
}
$previous = $number;
}
$result[] = $consecutiveArray;
// Get length of each sub array
$count = array_map('count', $result);
$first = reset($count);
$last = end($count);
prettyarray($result);
}
I want to extract data from the array to look like this showing the first and last numbers in the range:
19.0001.2 1 - 12
The above array should produce 15 different results with 9 of them showing no range of numbers because they are singular.
ie: 19.0001.8 4 40

Splitting array to use with jquery flot

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

Retrieving elements from a nested array

I am using MultipleIterator() to iterate through two different arrays and get each element.
My code
$d = new MultipleIterator ();
$d->attachIterator ( new ArrayIterator ( $tbl_one_data ) );
$d->attachIterator ( new ArrayIterator ( $tbl_two_data ) );
foreach ( $d as $data ) {
print_r($data);
}
Which generates the following :
My question is how do I loop through the array and return each element? For example I would like to return 2014-11-06 11:31:58.781018. Tried using $data[0][0] but this returns all elements in the first index but I only want one element.
EDIT
print_r($d);
MultipleIterator Object ( [storage:SplObjectStorage:private] => Array ( [00000000583bd67b000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => 1 [1] => 2014-11-06 11:31:58.781018 [2] => NONE [3] => NONE ) [1] => Array ( [0] => 2 [1] => 2014-11-06 11:31:58.799436 [2] => MANAGER [3] => 500 ) [2] => Array ( [0] => 3 [1] => 2014-11-06 11:31:58.841035 [2] => MANAGER [3] => 501 ) [3] => Array ( [0] => 4 [1] => 2014-11-06 11:33:00.741873 [2] => MANAGER [3] => 500 ) [4] => Array ( [0] => 5 [1] => 2014-11-06 11:33:00.802389 [2] => MANAGER [3] => 501 ) [5] => Array ( [0] => 6 [1] => 2014-11-06 13:15:49.457646 [2] => MANAGER [3] => 500 ) [6] => Array ( [0] => 7 [1] => 2014-11-06 13:37:16.259128 [2] => NONE [3] => NONE ) [7] => Array ( [0] => 8 [1] => 2014-11-06 13:37:16.275201 [2] => NONE [3] => 500 ) [8] => Array ( [0] => 9 [1] => 2014-11-06 13:37:27.682873 [2] => NONE [3] => NONE ) [9] => Array ( [0] => 10 [1] => 2014-11-06 13:37:27.690863 [2] => NONE [3] => 500 ) [10] => Array ( [0] => 11 [1] => 2014-11-06 13:52:21.108003 [2] => MANAGER [3] => 500 ) [11] => Array ( [0] => 12 [1] => 2014-11-06 14:17:01.266769 [2] => NONE [3] => NONE ) [12] => Array ( [0] => 13 [1] => 2014-11-06 14:17:01.279507 [2] => node1-1415283420.0 [3] => 500 ) [13] => Array ( [0] => 14 [1] => 2014-11-06 14:17:02.527183 [2] => node1-1415283420.0 [3] => 500 ) [14] => Array ( [0] => 15 [1] => 2014-11-06 14:17:23.775279 [2] => node1-1415283442.1 [3] => 500 ) ) ) [inf] => ) [00000000583bd67a000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [1] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [2] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [3] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [4] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [5] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [6] => Array ( [0] => Local/120#disc-agents/n [1] => PAUSEALL [2] => [3] => [4] => [5] => [6] => ) [7] => Array ( [0] => Dunc Test [1] => PAUSE [2] => [3] => [4] => [5] => [6] => ) [8] => Array ( [0] => Local/120#disc-agents/n [1] => UNPAUSEALL [2] => [3] => [4] => [5] => [6] => ) [9] => Array ( [0] => Dunc Test [1] => UNPAUSE [2] => [3] => [4] => [5] => [6] => ) [10] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [11] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [12] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) [13] => Array ( [0] => NONE [1] => ABANDON [2] => 1 [3] => 1 [4] => 1 [5] => [6] => ) [14] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) ) ) [inf] => ) ) )
if this is really output by print_r then $data[0][1] must be '2014-11-06 11:31:58.781018' . I test it by code:
$data = array(array('1','2014-11-06 11:31:58.781018'));
echo '<pre>';
print_r($data); echo '<br>';
echo 'what we want: '.$data[0][1].'<br>';
otput:
Array
(
[0] => Array
(
[0] => 1
[1] => 2014-11-06 11:31:58.781018
)
)
what we want: 2014-11-06 11:31:58.781018
If you have the $data as an array you can access the value: 2014-11-06 11:31:58.781018 by using,
$date = $data[0][1];

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.

Is this the correct syntax for 'array_unique()' in PHP?

I want to get rid of duplicates in my array but I'm still printing duplicates with this:
$getuser = ltrim($top10['url'], " users/" ); //trim url to get user id
$array[$i++] = $getuser;
$dirty = $array;
$clean = array_unique($dirty);
print_r($clean)."<br />";
Input print_r($array)
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Output print_r($clean);
Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
Yes, it seems to be correct. Basically you specify an array to array_unique and it gives you unique items out of the array.
$unique_array = array_unique($your_array);

Categories