foreach array using blade - php

I started using Blade template engine with codeigniter, and Google Api
i use blade to develope an old project created by another developers for this reason i have to keep the same blade data structure.
this is the Blade code written by a different developpers (i need to keep it ):
#foreach ($analytics['countries'] as $country)
['{{$country['country']}}', {{$country['visits_percent']}}],
#endforeach
this is my PHP code
$this->blade->view()->make('analytics/analytics', ['analytics' => $analytics]);
and this is my $analtics array :
Array
(
[0] => Array
(
[0] => Canada
[1] => 3367
)
[1] => Array
(
[0] => United States
[1] => 202
)
[2] => Array
(
[0] => Malaysia
[1] => 34
)
[3] => Array
(
[0] => Mexico
[1] => 31
)
[4] => Array
(
[0] => Peru
[1] => 23
)
[5] => Array
(
[0] => Brazil
[1] => 21
)
[6] => Array
(
[0] => United Kingdom
[1] => 21
)
[7] => Array
(
[0] => Netherlands
[1] => 17
)
[8] => Array
(
[0] => Nepal
[1] => 14
)
[9] => Array
(
[0] => India
[1] => 12
)
[10] => Array
(
[0] => Belarus
[1] => 11
)
[11] => Array
(
[0] => France
[1] => 9
)
[12] => Array
(
[0] => Ireland
[1] => 9
)
[13] => Array
(
[0] => Germany
[1] => 7
)
[14] => Array
(
[0] => Philippines
[1] => 6
)
[15] => Array
(
[0] => Singapore
[1] => 5
)
[16] => Array
(
[0] => Bangladesh
[1] => 4
)
[17] => Array
(
[0] => Italy
[1] => 4
)
[18] => Array
(
[0] => Serbia
[1] => 4
)
[19] => Array
(
[0] => Australia
[1] => 3
)
[20] => Array
(
[0] => Belgium
[1] => 3
)
[21] => Array
(
[0] => Indonesia
[1] => 3
)
[22] => Array
(
[0] => Iran
[1] => 3
)
[23] => Array
(
[0] => Jordan
[1] => 3
)
[24] => Array
(
[0] => Morocco
[1] => 3
)
[25] => Array
(
[0] => China
[1] => 2
)
[26] => Array
(
[0] => Colombia
[1] => 2
)
[27] => Array
(
[0] => Moldova
[1] => 2
)
[28] => Array
(
[0] => Pakistan
[1] => 2
)
[29] => Array
(
[0] => Poland
[1] => 2
)
[30] => Array
(
[0] => Romania
[1] => 2
)
[31] => Array
(
[0] => South Africa
[1] => 2
)
[32] => Array
(
[0] => Thailand
[1] => 2
)
[33] => Array
(
[0] => Turkey
[1] => 2
)
[34] => Array
(
[0] => Argentina
[1] => 1
)
[35] => Array
(
[0] => El Salvador
[1] => 1
)
[36] => Array
(
[0] => Hong Kong
[1] => 1
)
[37] => Array
(
[0] => Japan
[1] => 1
)
[38] => Array
(
[0] => Kenya
[1] => 1
)
[39] => Array
(
[0] => Latvia
[1] => 1
)
[40] => Array
(
[0] => New Zealand
[1] => 1
)
[41] => Array
(
[0] => Russia
[1] => 1
)
[42] => Array
(
[0] => Saudi Arabia
[1] => 1
)
[43] => Array
(
[0] => South Korea
[1] => 1
)
)
my question is how can i produce the same array structure for blade
thank you

I hope this code useful for you :
Route::get('/test', function (Request $request) {
$analytics=[
[
'Canada',
3367
],
[
'United States',
202
],
[
'Malaysia',
34
],
];
return view('welcome',compact('analytics'));
})
and you can use this code in view :
#foreach( $analytics as $analytic)
<p>
country = {{$analytic[0]}}
<br>
code = {{$analytic[1]}}
<tr>
</p>
#endforeach

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

Reading the response of Google Analytics php Api

I am trying to get the page views from each country.
I have following code for this purpose.
function getResults($analytics, $profileId,$url) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
$optParams = array(
'dimensions' => 'ga:country',
'filters' => 'ga:pagePath=#/605-2/'
);
return $analytics->data_ga->get(
'ga:' . $profileId,
'2017-11-01',
'today',
'ga:pageViews',
$optParams);
}
The code is working fine. The problem is I am not sure how to print the results as a table of form
Country | Views
USA | 52
Australia | 50
Below is the response I managed to filter out
Array ( [0] => Array ( [0] => (not set) [1] => 1 ) [1] => Array ( [0] => Australia [1] => 11 ) [2] => Array ( [0] => Bahrain [1] => 2 ) [3] => Array ( [0] => Belgium [1] => 1 ) [4] => Array ( [0] => Canada [1] => 5 ) [5] => Array ( [0] => Denmark [1] => 1 ) [6] => Array ( [0] => Finland [1] => 1 ) [7] => Array ( [0] => France [1] => 1 ) [8] => Array ( [0] => Germany [1] => 10 ) [9] => Array ( [0] => Hungary [1] => 1 ) [10] => Array ( [0] => Italy [1] => 1 ) [11] => Array ( [0] => Japan [1] => 4 ) [12] => Array ( [0] => Mali [1] => 1 ) [13] => Array ( [0] => Norway [1] => 1 ) [14] => Array ( [0] => Pakistan [1] => 589 ) [15] => Array ( [0] => Peru [1] => 1 ) [16] => Array ( [0] => Saudi Arabia [1] => 13 ) [17] => Array ( [0] => Singapore [1] => 1 ) [18] => Array ( [0] => South Africa [1] => 1 ) [19] => Array ( [0] => South Korea [1] => 2 ) [20] => Array ( [0] => Ukraine [1] => 1 ) [21] => Array ( [0] => United Arab Emirates [1] => 7 ) [22] => Array ( [0] => United Kingdom [1] => 7 ) [23] => Array ( [0] => United States [1] => 10 ) )

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>";
});

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