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
)
)
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 months ago.
I have code like this
$years_of_service = mysqli_query($mysqli, "SELECT YEAR(curdate())-Year_Entry FROM result");
$result = mysqli_fetch_all($years_of_service,MYSQLI_NUM);
When i try to echo that result to see what happened with that code, i found this kind of array
Array ( [0] => Array ( [0] => 25 ) [1] => Array ( [0] => 25 ) [2] => Array ( [0] => 25 ) [3] => Array ( [0] => 25 ) [4] => Array ( [0] => 25 ) [5] => Array ( [0] => 14 ) [6] => Array ( [0] => 14 ) [7] => Array ( [0] => 14 ) [8] => Array ( [0] => 14 ) [9] => Array ( [0] => 14 ) [10] => Array ( [0] => 12 ) [11] => Array ( [0] => 12 ) [12] => Array ( [0] => 12 ) [13] => Array ( [0] => 12 ) [14] => Array ( [0] => 12 ) [15] => Array ( [0] => 11 ) [16] => Array ( [0] => 11 ) [17] => Array ( [0] => 11 ) [18] => Array ( [0] => 11 ) [19] => Array ( [0] => 11 ) [20] => Array ( [0] => 10 ) [21] => Array ( [0] => 10 ) [22] => Array ( [0] => 10 ) [23] => Array ( [0] => 10 ) [24] => Array ( [0] => 10 ) [25] => Array ( [0] => 9 ) [26] => Array ( [0] => 9 ) [27] => Array ( [0] => 9 ) [28] => Array ( [0] => 9 ) [29] => Array ( [0] => 8 ) [30] => Array ( [0] => 8 ) [31] => Array ( [0] => 8 ) [32] => Array ( [0] => 8 ) [33] => Array ( [0] => 7 ) [34] => Array ( [0] => 7 ) [35] => Array ( [0] => 7 ) [36] => Array ( [0] => 7 ) [37] => Array ( [0] => 6 ) [38] => Array ( [0] => 6 ) [39] => Array ( [0] => 6 ) [40] => Array ( [0] => 6 ) [41] => Array ( [0] => 6 ) [42] => Array ( [0] => 6 ) [43] => Array ( [0] => 6 ) [44] => Array ( [0] => 6 ) [45] => Array ( [0] => 6 ) [46] => Array ( [0] => 5 ) [47] => Array ( [0] => 5 ) [48] => Array ( [0] => 5 ) [49] => Array ( [0] => 5 ) [50] => Array ( [0] => 5 ) [51] => Array ( [0] => 5 ) [52] => Array ( [0] => 5 ) [53] => Array ( [0] => 5 ) [54] => Array ( [0] => 5 ) [55] => Array ( [0] => 4 ) [56] => Array ( [0] => 4 ) [57] => Array ( [0] => 4 ) [58] => Array ( [0] => 4 ) [59] => Array ( [0] => 4 ) [60] => Array ( [0] => 4 ) [61] => Array ( [0] => 4 ) [62] => Array ( [0] => 4 ) [63] => Array ( [0] => 4 ) [64] => Array ( [0] => 4 ) [65] => Array ( [0] => 4 ) [66] => Array ( [0] => 4 ) [67] => Array ( [0] => 4 ) [68] => Array ( [0] => 4 ) [69] => Array ( [0] => 3 ) [70] => Array ( [0] => 3 ) [71] => Array ( [0] => 3 ) [72] => Array ( [0] => 3 ) [73] => Array ( [0] => 3 ) [74] => Array ( [0] => 3 ) [75] => Array ( [0] => 3 ) [76] => Array ( [0] => 3 ) [77] => Array ( [0] => 3 ) [78] => Array ( [0] => 2 ) [79] => Array ( [0] => 2 ) [80] => Array ( [0] => 2 ) [81] => Array ( [0] => 2 ) [82] => Array ( [0] => 1 ) [83] => Array ( [0] => 1 ) [84] => Array ( [0] => 1 ) [85] => Array ( [0] => 1 ) [86] => Array ( [0] => 0 ) [87] => Array ( [0] => 0 ) [88] => Array ( [0] => 0 ) [89] => Array ( [0] => 0 ) )
I want to make it become a simple array containing only its value like
Array(25,25,25,....)
What is the best approach i can use. I've tried to make it simple array with this
$simple = array_values($result);
But it did'nt work. It show the same output with $result. I will use it for map and reduce function to categorize years of service by specific range.
If PHP > 5.5.0 you can simply use array_column like as
$result = array_column($your_array,0);
or you can simply use call_user_func_array like as
call_user_func_array('array_merge',$your_array);
Demo
The reason this is happening is you're retrieving all rows (outer array) which are being returned as an array of columns (the inner arrays).
What you need to do is loop through the rows:
$years_of_service = mysqli_query($mysqli, "SELECT YEAR(curdate())-Year_Entry FROM result");
$result = mysqli_fetch_all($work_period,MYSQLI_NUM);
$data = [];
foreach ($result as $row) {
$data[] = $row[0];
}
Your array will now be 'flat':
$data => [
0 => 25,
1 => 24,
...
];
which will now return what you want when you use array_values:
$simple = array_values($data); // $simple will now equal Array(25, 24, ...)
You could try something like:
$simple = array();
foreach ($result as $value) {
$simple[] = $value[0];
}
I researched a lot of questions and answers posted on stackoverflow about the subject but I still couldn't find a solution to the following problem.
I need to generate an array from the text provided by this link, minus some specific lines: https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf. This part I was able to do.
PHP Code:
$arr = file('https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf');
$arr = preg_replace('/^(?![a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}\s).*$/i', '', $arr);
$arr = preg_replace ('/#/i', '',$arr);
$arr = preg_replace('/\t+/', ' ', $arr);
$arr = preg_replace('/\s{2,}/', ' ', $arr);
$arr = preg_replace('/ /', '~', $arr, 2);
foreach ($arr as $field)
{
$macList[] = explode('~', $field);
}
echo "<pre>";
print_r($macList);
echo "</pre>";
The problem is that when I use print_r, the first 52 sub-arrays are empty, plus some dozens at the bottom. I can't figure out how to remove them.
Reduced print_r output, because the full output returns 25770 sub-arrays:
Array
(
[0] => Array
(
[0] =>
)
[1] => Array
(
[0] =>
)
[2] => Array
(
[0] =>
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
[5] => Array
(
[0] =>
)
[6] => Array
(
[0] =>
)
[7] => Array
(
[0] =>
)
[8] => Array
(
[0] =>
)
[9] => Array
(
[0] =>
)
[10] => Array
(
[0] =>
)
[11] => Array
(
[0] =>
)
[12] => Array
(
[0] =>
)
[13] => Array
(
[0] =>
)
[14] => Array
(
[0] =>
)
[15] => Array
(
[0] =>
)
[16] => Array
(
[0] =>
)
[17] => Array
(
[0] =>
)
[18] => Array
(
[0] =>
)
[19] => Array
(
[0] =>
)
[20] => Array
(
[0] =>
)
[21] => Array
(
[0] =>
)
[22] => Array
(
[0] =>
)
[23] => Array
(
[0] =>
)
[24] => Array
(
[0] =>
)
[25] => Array
(
[0] =>
)
[26] => Array
(
[0] =>
)
[27] => Array
(
[0] =>
)
[28] => Array
(
[0] =>
)
[29] => Array
(
[0] =>
)
[30] => Array
(
[0] =>
)
[31] => Array
(
[0] =>
)
[32] => Array
(
[0] =>
)
[33] => Array
(
[0] =>
)
[34] => Array
(
[0] =>
)
[35] => Array
(
[0] =>
)
[36] => Array
(
[0] =>
)
[37] => Array
(
[0] =>
)
[38] => Array
(
[0] =>
)
[39] => Array
(
[0] =>
)
[40] => Array
(
[0] =>
)
[41] => Array
(
[0] =>
)
[42] => Array
(
[0] =>
)
[43] => Array
(
[0] =>
)
[44] => Array
(
[0] =>
)
[45] => Array
(
[0] =>
)
[46] => Array
(
[0] =>
)
[47] => Array
(
[0] =>
)
[48] => Array
(
[0] =>
)
[49] => Array
(
[0] =>
)
[50] => Array
(
[0] =>
)
[51] => Array
(
[0] =>
)
[52] => Array
(
[0] => 00:00:00
[1] => 00:00:00
[2] => Officially Xerox, but 0:0:0:0:0:0 is more common
)
)
Question: How do I remove these 52 (0 to 51) empty arrays? Nothing I tried worked.
Rather than trying to remove the empty keys, instead dont add the empty arrays in the 1st place:
EDIT is seems the 'empty' arrays are not in fact empty, but instead contain a single element with a space character. To remove you can combine empty with trim , array_map and array_filter:
foreach ($arr as $field)
{
$temp = explode('~', $field);
if(!empty(array_filter(array_map('trim', $temp))))
{
$macList[] = $temp;
}
}
echo "<pre>";
print_r($macList);
echo "</pre>";
As an example of using array_filter() and assuming it is always an empty single element array:
$new_array = array_filter($array, function($elements) {
return !empty($elements[0]);
});