Recursively remove elements from nested array looking for a specific value - php
I have the following nested array:
Array (
[0] => Array ( [0] => [1] => 51.212342,6.7834665 )
[1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 )
[2] => Array ( [0] => 28.8.2013 15:11:53 [1] => 0,0 )
[3] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 )
[4] => Array ( [0] => 28.8.2013 15:36:06 [1] => 0,0 )
[5] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 )
[6] => Array ( [0] => 28.8.2013 15:40:14 [1] => 0,0 )
[7] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 )
[8] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 )
[9] => Array ( [0] => 28.8.2013 16:11:20 [1] => 0,0 )
[10] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 )
[11] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 )
[12] => Array ( [0] => 28.8.2013 18:11:34 [1] => 0,0 )
[13] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 )
[14] => Array ( [0] => 28.8.2013 18:11:55 [1] => 0,0 )
[15] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 )
...
);
Here I need to remove all the elements that have "0,0" as their [$n][1] value. I tried this but some of the "0,0" are still there. Why?
for ($i = 0; $i < sizeof($locations); $i++) {
$key = array_search('0,0', $locations[$i]);
if ($key !== false) {
unset($locations[$i]);
$locations = array_values($locations);
}
}
Array (
[0] => Array ( [0] => [1] => 51.212342,6.7834665 )
[1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 )
[2] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 )
[3] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 )
[4] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 )
[5] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 )
[6] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 )
[7] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 )
[8] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 )
[9] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 )
[10] => Array ( [0] => 29.8.2013 10:07:56 [1] => 51.2123948,6.7834622 )
[11] => Array ( [0] => 29.8.2013 11:57:45 [1] => 51.21244537,6.78355515 )
[12] => Array ( [0] => 29.8.2013 11:58:27 [1] => 51.21238401,6.78352698 )
[13] => Array ( [0] => 29.8.2013 12:01:17 [1] => 51.2124044633333,6.78353637 )
[14] => Array ( [0] => 29.8.2013 12:11:18 [1] => 51.2124044633333,0.783536 )
[15] => Array ( [0] => 29.8.2013 12:12:39 [1] => 51.212416045,6.783523 )
...
);
One problem with your code is that sizeof($locations) changes every time you do a unset. So whenever you have two consecutive [$n][1] having "0,0", you are not able to detect that. Also your code is not looking at just[$n][1], its looking at all indexes in [$n][]
Use the below code:
$count = count($locations);
for ($i = 0; $i < $count; $i++) {
if ($locations[$i][1] == "0,0") {
unset($locations[$index]);
}
}
$locations = array_values($locations);
foreach ($locations as $index => $row) {
if ($locations[$index][1] == "0,0") unset($locations[$index]);
}
$locations = array_values($locations);
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 ) )
php merge duplicate values in a array
I apologize for not being word-perfect in English. I have this result from a foreach loop in php. my file is jason. Merging more value into one array Array ( [777565] => Array ( [0] => Array ( [0] => 777565-1 [1] => 777565-2 ) [1] => Array ( [0] => 777565-3 [1] => 777565-4 ) [2] => Array ( [0] => 777565-5 [1] => 777565-6 ) ) [777566] => Array ( [0] => Array ( [0] => 777566-1 [1] => 777566-2 ) [1] => Array ( [0] => 777566-3 [1] => 777566-4 ) [2] => Array ( [0] => 777566-5 [1] => 777566-6 ) ) ) but, I want Something like this: Array ( [777565] => Array ( [0] => 777565-1 [1] => 777565-2 [2] => 777565-3 [3] => 777565-4 [4] => 777565-5 [5] => 777565-6 ) [777566] => Array ( [0] => 777566-1 [1] => 777566-2 [2] => 777566-3 [3] => 777566-4 [4] => 777566-5 [5] => 777566-6 ) ) I tried hard and searched the internet but I could not find any way. Of course, I have the ability to move it to the database first and then to the array, but I think there should be a faster way. What do you think? thanks for reply.
If you have no problem looping through it and flatten the array according to your desire then you can try this: $parent = Array ( [777565] => Array ( [0] => Array ( [0] => 777565-1 [1] => 777565-2 ) [1] => Array ( [0] => 777565-3 [1] => 777565-4 ) [2] => Array ( [0] => 777565-5 [1] => 777565-6 ) ) [777566] => Array ( [0] => Array ( [0] => 777566-1 [1] => 777566-2 ) [1] => Array ( [0] => 777566-3 [1] => 777566-4 ) [2] => Array ( [0] => 777566-5 [1] => 777566-6 ) ) ); $length = count($parent); $result=[]; for($i=0; $i<$length; $i++){ for($j=0; $j<3; $j++){ $l=0; for($k=0; $k<2; $k++){ $result[777565+$i][$j][$l++] = $parent[777565+$i][$j][$k]; } } }
i only want to have array without null values (i have used empety(), isset(), is_null); below is my array and code that i have implemented toget array
Below array is formed when i fetch data from xsl sheet i have used empty(), isset(), is_null but all are not working bellow is my code for($i = 0; $i < count($newSheetData); $i++){ if($newSheetData[$i][0] == ''){ unset($newSheetData[$i]); }else{ //unset($newSheetData[$i]); } } This is my output: <pre/>Array ( [0] => Array ( [0] => quetion [1] => option1 [2] => option2 ) [1] => Array ( [0] => What [1] => a [2] => b ) [2] => Array ( [0] => now [1] => a [2] => b ) [3] => Array ( [0] => What [1] => a [2] => b ) [4] => Array ( [0] => [1] => [2] => ) [5] => Array ( [0] => [1] => [2] => ) [6] => Array ( [0] => [1] => [2] => ) [7] => Array ( [0] => [1] => [2] => ) [8] => Array ( [0] => [1] => [2] => ) [9] => Array ( [0] => [1] => [2] => ) [10] => Array ( [0] => [1] => [2] => ) ) ) i want this type of array Array ( [0] => Array ( [0] => quetion [1] => option1 [2] => option2 ) [1] => Array ( [0] => What [1] => a [2] => b ) [2] => Array ( [0] => now [1] => a [2] => b ) [3] => Array ( [0] => What [1] => a [2] => b )
foreach ($newSheetData as $key => $value) { $dataFiltred = array_filter($value, function($var) { if ($var === 0 || $var === '0' || !empty($var)) { return true; } return false; }); if (empty($dataFiltred)) { unset($newSheetData[$key]); } }
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 sum values via the same key and group by other key
I'd like to sum products by the same ref number, but I have 3rd parameter like dimension e.g. (..., 24, 26mm,...) and I can't sum this values only when they have the same dimension. I tried this: Group a multidimensional array by a particular value? but how to sum values? My array looks like this: Array ( [0] => Array ( [0] => 2 [1] => 790180X [2] => 26mm ) [1] => Array ( [0] => 4 [1] => 762182Z ) [2] => Array ( [0] => 2 [1] => 072182X ) [3] => Array ( [0] => 4 [1] => 660122Y ) [4] => Array ( [0] => 2 [1] => 790180X [2] => 24mm ) [5] => Array ( [0] => 1 [1] => 225160Y ) [6] => Array ( [0] => 1 [1] => 244160Y ) [7] => Array ( [0] => 1 [1] => 225160Y ) [8] => Array ( [0] => 8 [1] => 954120Y ) [9] => Array ( [0] => 3 [1] => 072182X ) ) I'd like to something like this: Array ( [0] => Array ( [0] => 2 [1] => 790180X [2] => 26mm ) [1] => Array ( [0] => 2 [1] => 790180X [2] => 24mm ) [2] => Array ( [0] => 4 [1] => 762182Z ) [3] => Array ( [0] => 5 [1] => 072182X ) [4] => Array ( [0] => 4 [1] => 660122Y ) [5] => Array ( [0] => 2 [1] => 225160Y ) [6] => Array ( [0] => 1 [1] => 244160Y ) [7] => Array ( [0] => 8 [1] => 954120Y ) ) The array in short version: Array => TO => Array ( ( [0] => 2:790180X:26mm [0] => 2:790180X:26mm [1] => 4:762182Z [1] => 2:790180X:24mm [2] => 2:072182X [2] => 4:762182Z [3] => 4:660122Y [3] => 5:072182X [4] => 2:790180X:24mm [4] => 4:660122Y [5] => 1:225160Y [5] => 2:225160Y [6] => 1:244160Y [6] => 1:244160Y [7] => 1:225160Y [7] => 8:954120Y [8] => 8:954120Y ) [9] => 3:072182X )
Just sum into a new array, using the various "grouping" fields as keys in the new array: $sums = array(); foreach ($yourarray as $element) { $sums[$element['dimension1']][$element['dimension2']]++; } For every dimension you need to 'group' by, you add a key to the $sums array.