echo multidimensional array values into a readable format - php

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

Related

Convert string format to LatLng format using PHP

I'm trying to draw polyline in a map, totally new to PHP,I tried already researched previous topic but their format doesn't match to the one I need or using other dev language.
How to convert $polylines_data as string
$polylines_data="9.76716,118.74789000000001,9.76665,118.74787,9.766100000000002,118.74787,9.76609,118.74773,9.76609,118.74745000000001,9.76519,118.74742,9.7644,118.74737,9.76382,118.74734000000001,9.7626,118.74728,9.76125,118.7472,9.760670000000001,118.74719,9.760280000000002,118.74724,9.75932,118.74741000000002,9.75826,118.74757000000001,9.75787,118.74763000000002,9.7575,118.74766000000001,9.757290000000001,118.74765000000001,9.756670000000002,118.74752000000001,9.755870000000002,118.74736000000001,9.75567,118.74732000000002,9.755320000000001,118.74732000000002,9.75441,118.74749000000001,9.753960000000001,118.74759000000002,9.753210000000001,118.74776000000001,9.752730000000001,118.74778,9.752510000000001,118.74777,9.75197,118.74773,9.75145,118.74772000000002,9.750720000000001,118.74776000000001,9.75051,118.74777,9.749300000000002,118.74785000000001,9.749270000000001,118.74863,9.74925,118.74941000000001,9.74914,118.75251000000002,9.747770000000001,118.75248,9.74609,118.75245000000001,9.74606,118.75307000000001,9.746080000000001,118.75314000000002,9.746120000000001,118.75316000000001,9.74624,118.75316000000001";
into this format or set of orders using PHP 👇
[
[-122.483696, 37.833818],
[-122.483482, 37.833174],
[-122.483396, 37.8327],
[-122.483568, 37.832056],
[-122.48404, 37.831141],
[-122.48404, 37.830497],
[-122.483482, 37.82992],
[-122.483568, 37.829548],
[-122.48507, 37.829446],
[-122.4861, 37.828802],
[-122.486958, 37.82931],
[-122.487001, 37.830802],
[-122.487516, 37.831683],
[-122.488031, 37.832158],
[-122.488889, 37.832971],
[-122.489876, 37.832632],
[-122.490434, 37.832937],
[-122.49125, 37.832429],
[-122.491636, 37.832564],
[-122.492237, 37.833378],
[-122.493782, 37.833683]
]
I'm trying to implement it here on client's end, all I need is the format from the backend end using PHP.
map.addSource('route', {
'type': 'geojson',
'data': {
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'LineString',
'coordinates': [
[-122.483696, 37.833818],
[-122.483482, 37.833174],
[-122.483396, 37.8327],
[-122.483568, 37.832056],
[-122.48404, 37.831141],
[-122.48404, 37.830497],
[-122.483482, 37.82992],
[-122.483568, 37.829548],
[-122.48507, 37.829446],
[-122.4861, 37.828802],
[-122.486958, 37.82931],
[-122.487001, 37.830802],
[-122.487516, 37.831683],
[-122.488031, 37.832158],
[-122.488889, 37.832971],
[-122.489876, 37.832632],
[-122.490434, 37.832937],
[-122.49125, 37.832429],
[-122.491636, 37.832564],
[-122.492237, 37.833378],
[-122.493782, 37.833683]
]
}
}
});
Really appreciate if you can help. Thanks in advance. 🙌
You could use explode() and foreach over result to get what you want:
<?php
$polylines_data="9.76716,118.74789000000001,9.76665,118.74787,9.766100000000002,118.74787,9.76609,118.74773,9.76609,118.74745000000001,9.76519,118.74742,9.7644,118.74737,9.76382,118.74734000000001,9.7626,118.74728,9.76125,118.7472,9.760670000000001,118.74719,9.760280000000002,118.74724,9.75932,118.74741000000002,9.75826,118.74757000000001,9.75787,118.74763000000002,9.7575,118.74766000000001,9.757290000000001,118.74765000000001,9.756670000000002,118.74752000000001,9.755870000000002,118.74736000000001,9.75567,118.74732000000002,9.755320000000001,118.74732000000002,9.75441,118.74749000000001,9.753960000000001,118.74759000000002,9.753210000000001,118.74776000000001,9.752730000000001,118.74778,9.752510000000001,118.74777,9.75197,118.74773,9.75145,118.74772000000002,9.750720000000001,118.74776000000001,9.75051,118.74777,9.749300000000002,118.74785000000001,9.749270000000001,118.74863,9.74925,118.74941000000001,9.74914,118.75251000000002,9.747770000000001,118.75248,9.74609,118.75245000000001,9.74606,118.75307000000001,9.746080000000001,118.75314000000002,9.746120000000001,118.75316000000001,9.74624,118.75316000000001";
$coords = explode(',', $polylines_data);
$points = array();
for ($i = 0; $i < count($coords); $i += 2) {
$points[] = array((float)$coords[$i+1], (float)$coords[$i]);
}
echo '<pre>';
print_r($points);
Output is:
Array
(
[0] => Array
(
[0] => 118.74789
[1] => 9.76716
)
[1] => Array
(
[0] => 118.74787
[1] => 9.76665
)
[2] => Array
(
[0] => 118.74787
[1] => 9.7661
)
[3] => Array
(
[0] => 118.74773
[1] => 9.76609
)
[4] => Array
(
[0] => 118.74745
[1] => 9.76609
)
[5] => Array
(
[0] => 118.74742
[1] => 9.76519
)
[6] => Array
(
[0] => 118.74737
[1] => 9.7644
)
[7] => Array
(
[0] => 118.74734
[1] => 9.76382
)
[8] => Array
(
[0] => 118.74728
[1] => 9.7626
)
[9] => Array
(
[0] => 118.7472
[1] => 9.76125
)
[10] => Array
(
[0] => 118.74719
[1] => 9.76067
)
[11] => Array
(
[0] => 118.74724
[1] => 9.76028
)
[12] => Array
(
[0] => 118.74741
[1] => 9.75932
)
[13] => Array
(
[0] => 118.74757
[1] => 9.75826
)
[14] => Array
(
[0] => 118.74763
[1] => 9.75787
)
[15] => Array
(
[0] => 118.74766
[1] => 9.7575
)
[16] => Array
(
[0] => 118.74765
[1] => 9.75729
)
[17] => Array
(
[0] => 118.74752
[1] => 9.75667
)
[18] => Array
(
[0] => 118.74736
[1] => 9.75587
)
[19] => Array
(
[0] => 118.74732
[1] => 9.75567
)
[20] => Array
(
[0] => 118.74732
[1] => 9.75532
)
[21] => Array
(
[0] => 118.74749
[1] => 9.75441
)
[22] => Array
(
[0] => 118.74759
[1] => 9.75396
)
[23] => Array
(
[0] => 118.74776
[1] => 9.75321
)
[24] => Array
(
[0] => 118.74778
[1] => 9.75273
)
[25] => Array
(
[0] => 118.74777
[1] => 9.75251
)
[26] => Array
(
[0] => 118.74773
[1] => 9.75197
)
[27] => Array
(
[0] => 118.74772
[1] => 9.75145
)
[28] => Array
(
[0] => 118.74776
[1] => 9.75072
)
[29] => Array
(
[0] => 118.74777
[1] => 9.75051
)
[30] => Array
(
[0] => 118.74785
[1] => 9.7493
)
[31] => Array
(
[0] => 118.74863
[1] => 9.74927
)
[32] => Array
(
[0] => 118.74941
[1] => 9.74925
)
[33] => Array
(
[0] => 118.75251
[1] => 9.74914
)
[34] => Array
(
[0] => 118.75248
[1] => 9.74777
)
[35] => Array
(
[0] => 118.75245
[1] => 9.74609
)
[36] => Array
(
[0] => 118.75307
[1] => 9.74606
)
[37] => Array
(
[0] => 118.75314
[1] => 9.74608
)
[38] => Array
(
[0] => 118.75316
[1] => 9.74612
)
[39] => Array
(
[0] => 118.75316
[1] => 9.74624
)
)

Calculate the sum of columns with PHP [duplicate]

This question already has answers here:
How to calculate the sum of variables from JSON data?
(1 answer)
PHP Sum up JSON Object value
(1 answer)
Closed 2 years ago.
I would like to calculate the sum of the numbers with highlight yellow color in a variable and the sum of the other values which are not highlighted in another variable.
My PHP code is:
$symbols=$request->Get('/_get?symbol=JAN&from=1586959285&to=1586959324&data=ba');
if($symbols!=false) {
echo $symbols;
}
and the result of this code is:
{"retcode":"0 Done","answer":[[1.09076,1.09084],[1.09077,1.09086],[1.09072,1.09081],[1.09073,1.09082],[1.09073,1.09083],[1.09074,1.09084],[1.09082,1.09084],[1.09090,1.09097],[1.09091,1.09099],[1.09091,1.09100],[1.09093,1.09101],[1.09094,1.09102],[1.09095,1.09104],[1.09097,1.09106],[1.09098,1.09107],[1.09098,1.09108],[1.09099,1.09108],[1.09097,1.09106],[1.09096,1.09106],[1.09096,1.09105],[1.09097,1.09105],[1.09096,1.09105],[1.09095,1.09105],[1.09095,1.09104],[1.09093,1.09102],[1.09095,1.09102],[1.09095,1.09104],[1.09095,1.09103],[1.09093,1.09102],[1.09082,1.09090],[1.09082,1.09092],[1.09083,1.09092],[1.09082,1.09091],[1.09081,1.09090],[1.09080,1.09089],[1.09081,1.09090],[1.09080,1.09089],[1.09081,1.09089],[1.09080,1.09089],[1.09080,1.09088],[1.09078,1.09087],[1.09079,1.09088],[1.09079,1.09089],[1.09075,1.09084],[1.09074,1.09083],[1.09074,1.09084],[1.09074,1.09083],[1.09074,1.09084],[1.09074,1.09083],[1.09073,1.09083],[1.09074,1.09083],[1.09074,1.09084],[1.09074,1.09083],[1.09073,1.09083],[1.09073,1.09082],[1.09073,1.09083],[1.09074,1.09083],[1.09073,1.09082],[1.09073,1.09083],[1.09073,1.09082],[1.09072,1.09081],[1.09071,1.09081],[1.09072,1.09081],[1.09072,1.09083],[1.09073,1.09083]]}
If I decode the JSON my code is:
$hugeArray = json_decode($symbols, true);
foreach ($hugeArray as $key => $value)
{
print_r($hugeArray);
}
And the result is:
Array ( [retcode] => 0 Done [answer] => Array ( [0] => Array ( [0] => 1.09076 [1] => 1.09084 ) [1] => Array ( [0] => 1.09077 [1] => 1.09086 ) [2] => Array ( [0] => 1.09072 [1] => 1.09081 ) [3] => Array ( [0] => 1.09073 [1] => 1.09082 ) [4] => Array ( [0] => 1.09073 [1] => 1.09083 ) [5] => Array ( [0] => 1.09074 [1] => 1.09084 ) [6] => Array ( [0] => 1.09082 [1] => 1.09084 ) [7] => Array ( [0] => 1.0909 [1] => 1.09097 ) [8] => Array ( [0] => 1.09091 [1] => 1.09099 ) [9] => Array ( [0] => 1.09091 [1] => 1.091 ) [10] => Array ( [0] => 1.09093 [1] => 1.09101 ) [11] => Array ( [0] => 1.09094 [1] => 1.09102 ) [12] => Array ( [0] => 1.09095 [1] => 1.09104 ) [13] => Array ( [0] => 1.09097 [1] => 1.09106 ) [14] => Array ( [0] => 1.09098 [1] => 1.09107 ) [15] => Array ( [0] => 1.09098 [1] => 1.09108 ) [16] => Array ( [0] => 1.09099 [1] => 1.09108 ) [17] => Array ( [0] => 1.09097 [1] => 1.09106 ) [18] => Array ( [0] => 1.09096 [1] => 1.09106 ) [19] => Array ( [0] => 1.09096 [1] => 1.09105 ) [20] => Array ( [0] => 1.09097 [1] => 1.09105 ) [21] => Array ( [0] => 1.09096 [1] => 1.09105 ) [22] => Array ( [0] => 1.09095 [1] => 1.09105 ) [23] => Array ( [0] => 1.09095 [1] => 1.09104 ) [24] => Array ( [0] => 1.09093 [1] => 1.09102 ) [25] => Array ( [0] => 1.09095 [1] => 1.09102 ) [26] => Array ( [0] => 1.09095 [1] => 1.09104 ) [27] => Array ( [0] => 1.09095 [1] => 1.09103 ) [28] => Array ( [0] => 1.09093 [1] => 1.09102 ) [29] => Array ( [0] => 1.09082 [1] => 1.0909 ) [30] => Array ( [0] => 1.09082 [1] => 1.09092 ) [31] => Array ( [0] => 1.09083 [1] => 1.09092 ) [32] => Array ( [0] => 1.09082 [1] => 1.09091 ) [33] => Array ( [0] => 1.09081 [1] => 1.0909 ) [34] => Array ( [0] => 1.0908 [1] => 1.09089 ) [35] => Array ( [0] => 1.09081 [1] => 1.0909 ) [36] => Array ( [0] => 1.0908 [1] => 1.09089 ) [37] => Array ( [0] => 1.09081 [1] => 1.09089 ) [38] => Array ( [0] => 1.0908 [1] => 1.09089 ) [39] => Array ( [0] => 1.0908 [1] => 1.09088 ) [40] => Array ( [0] => 1.09078 [1] => 1.09087 ) [41] => Array ( [0] => 1.09079 [1] => 1.09088 ) [42] => Array ( [0] => 1.09079 [1] => 1.09089 ) [43] => Array ( [0] => 1.09075 [1] => 1.09084 ) [44] => Array ( [0] => 1.09074 [1] => 1.09083 ) [45] => Array ( [0] => 1.09074 [1] => 1.09084 ) [46] => Array ( [0] => 1.09074 [1] => 1.09083 ) [47] => Array ( [0] => 1.09074 [1] => 1.09084 ) [48] => Array ( [0] => 1.09074 [1] => 1.09083 ) [49] => Array ( [0] => 1.09073 [1] => 1.09083 ) [50] => Array ( [0] => 1.09074 [1] => 1.09083 ) [51] => Array ( [0] => 1.09074 [1] => 1.09084 ) [52] => Array ( [0] => 1.09074 [1] => 1.09083 ) [53] => Array ( [0] => 1.09073 [1] => 1.09083 ) [54] => Array ( [0] => 1.09073 [1] => 1.09082 ) [55] => Array ( [0] => 1.09073 [1] => 1.09083 ) [56] => Array ( [0] => 1.09074 [1] => 1.09083 ) [57] => Array ( [0] => 1.09073 [1] => 1.09082 ) [58] => Array ( [0] => 1.09073 [1] => 1.09083 ) [59] => Array ( [0] => 1.09073 [1] => 1.09082 ) [60] => Array ( [0] => 1.09072 [1] => 1.09081 ) [61] => Array ( [0] => 1.09071 [1] => 1.09081 ) [62] => Array ( [0] => 1.09072 [1] => 1.09081 ) [63] => Array ( [0] => 1.09072 [1] => 1.09083 ) [64] => Array ( [0] => 1.09073 [1] => 1.09083 ) ) ) Array ( [retcode] => 0 Done [answer] => Array ( [0] => Array ( [0] => 1.09076 [1] => 1.09084 ) [1] => Array ( [0] => 1.09077 [1] => 1.09086 ) [2] => Array ( [0] => 1.09072 [1] => 1.09081 ) [3] => Array ( [0] => 1.09073 [1] => 1.09082 ) [4] => Array ( [0] => 1.09073 [1] => 1.09083 ) [5] => Array ( [0] => 1.09074 [1] => 1.09084 ) [6] => Array ( [0] => 1.09082 [1] => 1.09084 ) [7] => Array ( [0] => 1.0909 [1] => 1.09097 ) [8] => Array ( [0] => 1.09091 [1] => 1.09099 ) [9] => Array ( [0] => 1.09091 [1] => 1.091 ) [10] => Array ( [0] => 1.09093 [1] => 1.09101 ) [11] => Array ( [0] => 1.09094 [1] => 1.09102 ) [12] => Array ( [0] => 1.09095 [1] => 1.09104 ) [13] => Array ( [0] => 1.09097 [1] => 1.09106 ) [14] => Array ( [0] => 1.09098 [1] => 1.09107 ) [15] => Array ( [0] => 1.09098 [1] => 1.09108 ) [16] => Array ( [0] => 1.09099 [1] => 1.09108 ) [17] => Array ( [0] => 1.09097 [1] => 1.09106 ) [18] => Array ( [0] => 1.09096 [1] => 1.09106 ) [19] => Array ( [0] => 1.09096 [1] => 1.09105 ) [20] => Array ( [0] => 1.09097 [1] => 1.09105 ) [21] => Array ( [0] => 1.09096 [1] => 1.09105 ) [22] => Array ( [0] => 1.09095 [1] => 1.09105 ) [23] => Array ( [0] => 1.09095 [1] => 1.09104 ) [24] => Array ( [0] => 1.09093 [1] => 1.09102 ) [25] => Array ( [0] => 1.09095 [1] => 1.09102 ) [26] => Array ( [0] => 1.09095 [1] => 1.09104 ) [27] => Array ( [0] => 1.09095 [1] => 1.09103 ) [28] => Array ( [0] => 1.09093 [1] => 1.09102 ) [29] => Array ( [0] => 1.09082 [1] => 1.0909 ) [30] => Array ( [0] => 1.09082 [1] => 1.09092 ) [31] => Array ( [0] => 1.09083 [1] => 1.09092 ) [32] => Array ( [0] => 1.09082 [1] => 1.09091 ) [33] => Array ( [0] => 1.09081 [1] => 1.0909 ) [34] => Array ( [0] => 1.0908 [1] => 1.09089 ) [35] => Array ( [0] => 1.09081 [1] => 1.0909 ) [36] => Array ( [0] => 1.0908 [1] => 1.09089 ) [37] => Array ( [0] => 1.09081 [1] => 1.09089 ) [38] => Array ( [0] => 1.0908 [1] => 1.09089 ) [39] => Array ( [0] => 1.0908 [1] => 1.09088 ) [40] => Array ( [0] => 1.09078 [1] => 1.09087 ) [41] => Array ( [0] => 1.09079 [1] => 1.09088 ) [42] => Array ( [0] => 1.09079 [1] => 1.09089 ) [43] => Array ( [0] => 1.09075 [1] => 1.09084 ) [44] => Array ( [0] => 1.09074 [1] => 1.09083 ) [45] => Array ( [0] => 1.09074 [1] => 1.09084 ) [46] => Array ( [0] => 1.09074 [1] => 1.09083 ) [47] => Array ( [0] => 1.09074 [1] => 1.09084 ) [48] => Array ( [0] => 1.09074 [1] => 1.09083 ) [49] => Array ( [0] => 1.09073 [1] => 1.09083 ) [50] => Array ( [0] => 1.09074 [1] => 1.09083 ) [51] => Array ( [0] => 1.09074 [1] => 1.09084 ) [52] => Array ( [0] => 1.09074 [1] => 1.09083 ) [53] => Array ( [0] => 1.09073 [1] => 1.09083 ) [54] => Array ( [0] => 1.09073 [1] => 1.09082 ) [55] => Array ( [0] => 1.09073 [1] => 1.09083 ) [56] => Array ( [0] => 1.09074 [1] => 1.09083 ) [57] => Array ( [0] => 1.09073 [1] => 1.09082 ) [58] => Array ( [0] => 1.09073 [1] => 1.09083 ) [59] => Array ( [0] => 1.09073 [1] => 1.09082 ) [60] => Array ( [0] => 1.09072 [1] => 1.09081 ) [61] => Array ( [0] => 1.09071 [1] => 1.09081 ) [62] => Array ( [0] => 1.09072 [1] => 1.09081 ) [63] => Array ( [0] => 1.09072 [1] => 1.09083 ) [64] => Array ( [0] => 1.09073 [1] => 1.09083 ) ) )
Any Suggestions? Thanks in advance.

looping through multidimensional array from post and save to mysql

This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise

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