I have this code below that I want to make the output like this json:
[{avatarUrl: '',messages:
[{time: ,text: ''},
{time: ,text: ''},
{time: ,text: ''}]}
,{own: true,messages:
[{time: ,text: ''},
{time: ,text: ''},
{time: ,text: ''}]}
,{avatarUrl: '',messages:
[{time: ,text: ''},
{time: ,text: ''},
{time: ,text: ''},
{time: ,text: ''},
{time: ,text: ''}]}
,{own: true,messages:
[{time: ,text: ''},
{time: ,text: ''},
{time: ,text: ''}]}]
Because on my current codes the output is this, I also added the exact result of my mysql fetch array :
[{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:52","text":"hellooo"}]},
{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:54","text":"hi"}]},
{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:56","text":"yes"}]},
{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:58","text":"ano yun"}]},
{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:32:20","text":"jjj"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:32:22","text":"kasdkaskds"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:47","text":"yes"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:50","text":" WHatzz"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:51","text":"up"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:52","text":"jejes"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:53","text":"s"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:53","text":"ssSs"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 ...
empfrom empto empmsg xdatetime xuser
------- ------ ---------- ------------------- --------
119 144 hellooo 2019-02-22 15:31:52 119
119 144 hi 2019-02-22 15:31:54 119
119 144 yes 2019-02-22 15:31:56 119
119 144 ano yun 2019-02-22 15:31:58 119
144 119 jjj 2019-02-22 15:32:20 144
144 119 kasdkaskds 2019-02-22 15:32:22 144
144 119 yes 2019-02-22 15:33:47 144
144 119 WHatzz 2019-02-22 15:33:50 144
144 119 up 2019-02-22 15:33:51 144
144 119 jejes 2019-02-22 15:33:52 144
144 119 s 2019-02-22 15:33:53 144
144 119 ssSs 2019-02-22 15:33:53 144
144 119 s 2019-02-22 15:33:53 144
144 119 s 2019-02-22 15:33:53 144
144 119 s 2019-02-22 15:33:53 144
144 119 s 2019-02-22 15:33:54 144
144 119 s 2019-02-22 15:33:54 144
144 119 sa 2019-02-22 15:33:54 144
144 119 a 2019-02-22 15:33:54 144
144 119 as 2019-02-22 15:33:54 144
144 119 d 2019-02-22 15:33:55 144
144 119 as 2019-02-22 15:33:56 144
144 119 jsjsd 2019-02-22 15:34:04 144
144 119 asd 2019-02-22 15:34:04 144
144 119 as 2019-02-22 15:34:04 144
144 119 a 2019-02-22 15:34:05 144
144 119 ajsjsjksd 2019-02-22 15:34:06 144
144 119 sdasd 2019-02-22 15:34:07 144
144 119 wew 2019-02-22 15:34:08 144
And this is my code. I already add some trappings to copycat the exact format but I cant still replicate. Any idea or do I neeed to add some condition.
$json_response_list = array();
$json_response = array();
$json_msgx = array();
$own = 0;
$from = 0;
$mydata = "";
$ctr = 1;
$cx = 1;
$sql = "SELECT empfrom,empto,empmsg,xdatetime,xuser FROM chatbox WHERE xdelete = 0 AND (empto = '".$_POST['userto']."' OR empfrom = '".$_POST['userto']."') AND (empto = '".$_SESSION['crmuserid']."' OR empfrom = '".$_SESSION['crmuserid']."' ) ORDER BY xdatetime ASC";
$result = mysqli_query($conn1,$sql) or die(mysqli_error($sql));
while ($row = mysqli_fetch_array($result)) {
if($row['xuser']==$_SESSION['crmuserid']){
if($own==0){
$json_response['own'] = true;
$own = 1;
$from = 0;
$cx = 1;
}
}else{
if($from==0){
$json_response['avatarUrl'] = "assets/img/avatars/avatar_02_tn.png";
$from = 1;
$own = 0;
$cx = 1;
}
}
$json_response['messages'] = array();
$json_msgx['time'] = $row['xdatetime'];
$json_msgx['text'] = $row['empmsg'];
array_push($json_response['messages'],$json_msgx);
if($ctr!=1 && $cx==1){
array_push($json_response_list,$json_response);
}
$ctr++;
$cx++;
}
$jsonData = json_encode($json_response_list); //JSON_PRETTY_PRINT
echo $jsonData;
I found out that the json format that I want to achieve to be output is not possible, specially inside a loop because it will automatically replicate the non existing element of array. Below is the sample code I made to make it work.
$json_response_list = array();
$json_response = array();
$json_msgx = array();
$own = 0;
$from = 0;
$mydata = "";
$ctr = 1;
$cx = 1;
$sql = "SELECT empfrom,empto,empmsg,xdatetime,xuser FROM chatbox WHERE xdelete = 0 AND (empto = '".$_POST['userto']."' OR empfrom = '".$_POST['userto']."') AND (empto = '".$_SESSION['crmuserid']."' OR empfrom = '".$_SESSION['crmuserid']."' ) ORDER BY xdatetime ASC";
$result = mysqli_query($conn1,$sql) or die(mysqli_error($sql));
while ($row = mysqli_fetch_array($result)) {
if($row['xuser']==$_SESSION['crmuserid']){
if($own==0){
if($cx!=1 && $ctr!="1"){
array_push($json_response_list,$json_response);
}
$json_response['avatarUrl'] = "";
$json_response['own'] = "1";
$own = 1;
$from = 0;
$cx = 1;
$json_response['messages'] = array();
}
}else{
if($from==0){
if($cx!=1 && $ctr!="1"){
array_push($json_response_list,$json_response);
}
$json_response['avatarUrl'] = "assets/img/avatars/avatar_02_tn.png";
$json_response['own'] = "";
$from = 1;
$own = 0;
$cx = 1;
$json_response['messages'] = array();
}
}
$json_msgx['time'] = $row['xuser'];
$json_msgx['text'] = $row['xuser'];
array_push($json_response['messages'],$json_msgx);
$ctr++;
$cx++;
}
$jsonData = json_encode($json_response_list); //JSON_PRETTY_PRINT
echo $jsonData;
Related
I have a problem trying using for loop inside a loop i can't get the result i want
<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".echo $sg;."</b>";
for($g = 1; $g <= 11; $g++){
echo "<p>".echo $g."</p>";
}
?>
I looking the result to be like
1
1 2 3 4 5 6 7 8 9 10 11
2
12 13 14 15 16 17 18 19 20
3
21 22 23 24 25 26 27 28 29 30
continuous ....
<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<br /> <b>". $sg."</b> ";
for($g = 1+(10*($sg-1)); $g <= (10*($sg-1))+10; $g++){
echo "". $g." ";
}
}
?>
Output:
1 1 2 3 4 5 6 7 8 9 10
2 11 12 13 14 15 16 17 18 19 20
3 21 22 23 24 25 26 27 28 29 30
4 31 32 33 34 35 36 37 38 39 40
5 41 42 43 44 45 46 47 48 49 50
6 51 52 53 54 55 56 57 58 59 60
7 61 62 63 64 65 66 67 68 69 70
8 71 72 73 74 75 76 77 78 79 80
9 81 82 83 84 85 86 87 88 89 90
10 91 92 93 94 95 96 97 98 99 100
11 101 102 103 104 105 106 107 108 109 110
<?php
$inner_start = 1;
$inner_max = 11;
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".$sg."</b></br>";
echo "<p>";
for($g = $inner_start; $g <= $inner_max; $g++){
echo $g . ' ';
}
$inner_start = $inner_start + 10;
$inner_max += 10;
echo "</p></br>";
}
?>
This is what I got:
<?php
// Program to print all
// combination of size r
// in an array of size n
function printCombinations($arr, $n, $r) {
$data = [];
combinationUtil($arr, $data, 0, $n - 1, 0, $r);
}
function combinationUtil($arr, $data, $start, $end, $index, $r) {
if ($index == $r) {
for ($j = 0; $j < $r; $j++) {
echo $data[$j];
}
echo "<br>";
return;
}
for ($i = $start; $i <= $end && $end - $i + 1 >= $r - $index; $i++) {
$data[$index] = $arr[$i];
combinationUtil($arr, $data, $i + 1, $end, $index + 1, $r);
}
}
$arr = [
1,
2,
3,
4,
5
];
$r = 3;
$n = count($arr);
printCombinations($arr, $n, $r);
and it gives this output:
123
124
125
134
135
145
234
235
245
345
And what I need is this:
123
124
125
132
134
135
142
143
145
152
153
154
213
214
215
231
234
235
241
243
245
251
253
254
312
314
315
321
324
325
341
342
345
351
352
354
412
413
414
415
421
423
425
431
432
435
451
452
453
512
513
514
521
523
524
531
532
534
541
542
543
You can use a recursive approach, that iterates over the array, and calls itself in each iteration by removing the current element, and prepending the returned variations with it.
Something like this:
<?php
function variation_without_repetition ($array,$items){
if($items == 0 || count($array) == 0) return [[]];
$variations = [];
foreach($array as $index => $item){
if(array_search($item, $array) < $index) continue;
$array_remaining = $array;
array_splice($array_remaining,$index,1);
foreach(variation_without_repetition($array_remaining,$items - 1) as $variation){
array_unshift($variation,$item);
$variations[] = $variation;
}
}
return $variations;
}
$variations = variation_without_repetition([1,2,3,4,5], 3);
foreach($variations as $variation){
echo implode($variation);
echo "<br>\n";
}
?>
How do I write the following switch case code using less lines? It is taking up a lot of space and lines but I'm unsure what's the better way of writing this.
This is a function where I am calculating the price which I have set in WordPress backend according to the various distance range.
if the distance is less than 300 miles, I have a fixed rate set in the backend for various distance range and if it is greater than 300 miles, then I want to use price per mile. The code does exactly what I need but I feel there can be more compact way of writing this?
Thanks in advance!
Following is the code:
function calculatePrice($distance_value_complete, $fixed_rate, $variable_rate) {
if (floatval($distance_value_complete) < 300 ):
switch (true) {
case floatval($distance_value_complete) < 2:
$vehicle_price1 = $fixed_rate['upto_2_mile'];
break;
case ( (floatval($distance_value_complete) >= 2) && (floatval($distance_value_complete) < 4) ):
$vehicle_price1 = $fixed_rate['from_2_to_4_miles'];
break;
case ( (floatval($distance_value_complete) >= 4) && (floatval($distance_value_complete) < 9) ):
$vehicle_price1 = $fixed_rate['from_4_to_9_miles'];
break;
case ( (floatval($distance_value_complete) >= 9) && (floatval($distance_value_complete) < 14) ):
$vehicle_price1 = $fixed_rate['from_9_to_14_miles'];
break;
case ( (floatval($distance_value_complete) >= 14) && (floatval($distance_value_complete) < 19) ):
$vehicle_price1 = $fixed_rate['from_14_to_19_miles'];
break;
case ( (floatval($distance_value_complete) >= 19) && (floatval($distance_value_complete) < 25) ):
$vehicle_price1 = $fixed_rate['from_19_to_25_miles'];
break;
case ( (floatval($distance_value_complete) >= 25) && (floatval($distance_value_complete) < 29) ):
$vehicle_price1 = $fixed_rate['from_25_to_29_miles'];
break;
case ( (floatval($distance_value_complete) >= 29) && (floatval($distance_value_complete) < 35) ):
$vehicle_price1 = $fixed_rate['from_29_to_35_miles'];
break;
case ( (floatval($distance_value_complete) >= 35) && (floatval($distance_value_complete) < 40) ):
$vehicle_price1 = $fixed_rate['from_35_to_40_miles'];
break;
case ( (floatval($distance_value_complete) >= 40) && (floatval($distance_value_complete) < 45) ):
$vehicle_price1 = $fixed_rate['from_40_to_45_miles'];
break;
case ( (floatval($distance_value_complete) >= 45) && (floatval($distance_value_complete) < 50) ):
$vehicle_price1 = $fixed_rate['from_45_to_50_miles'];
break;
case ( (floatval($distance_value_complete) >= 50) && (floatval($distance_value_complete) < 56) ):
$vehicle_price1 = $fixed_rate['from_50_to_56_miles'];
break;
case ( (floatval($distance_value_complete) >= 56) && (floatval($distance_value_complete) < 62) ):
$vehicle_price1 = $fixed_rate['from_56_to_62_miles'];
break;
case ( (floatval($distance_value_complete) >= 62) && (floatval($distance_value_complete) < 70) ):
$vehicle_price1 = $fixed_rate['from_62_to_70_miles'];
break;
case ( (floatval($distance_value_complete) >= 70) && (floatval($distance_value_complete) < 80) ):
$vehicle_price1 = $fixed_rate['from_70_to_80_miles'];
break;
case ( (floatval($distance_value_complete) >= 80) && (floatval($distance_value_complete) < 90) ):
$vehicle_price1 = $fixed_rate['from_80_to_90_miles'];
break;
case ( (floatval($distance_value_complete) >= 90) && (floatval($distance_value_complete) < 100) ):
$vehicle_price1 = $fixed_rate['from_90_to_100_miles'];
break;
case ( (floatval($distance_value_complete) >= 100) && (floatval($distance_value_complete) < 110) ):
$vehicle_price1 = $fixed_rate['from_100_to_110_miles'];
break;
case ( (floatval($distance_value_complete) >= 110) && (floatval($distance_value_complete) < 120) ):
$vehicle_price1 = $fixed_rate['from_110_to_120_miles'];
break;
case ( (floatval($distance_value_complete) >= 120) && (floatval($distance_value_complete) < 130) ):
$vehicle_price1 = $fixed_rate['from_120_to_130_miles'];
break;
case ( (floatval($distance_value_complete) >= 130) && (floatval($distance_value_complete) < 140) ):
$vehicle_price1 = $fixed_rate['from_130_to_140_miles'];
break;
case ( (floatval($distance_value_complete) >= 140) && (floatval($distance_value_complete) < 150) ):
$vehicle_price1 = $fixed_rate['from_140_to_150_miles'];
break;
case ( (floatval($distance_value_complete) >= 150) && (floatval($distance_value_complete) < 160) ):
$vehicle_price1 = $fixed_rate['from_150_to_160_miles'];
break;
case ( (floatval($distance_value_complete) >= 160) && (floatval($distance_value_complete) < 170) ):
$vehicle_price1 = $fixed_rate['from_160_to_170_miles'];
break;
case ( (floatval($distance_value_complete) >= 170) && (floatval($distance_value_complete) < 180) ):
$vehicle_price1 = $fixed_rate['from_170_to_180_miles'];
break;
case ( (floatval($distance_value_complete) >= 180) && (floatval($distance_value_complete) < 190) ):
$vehicle_price1 = $fixed_rate['from_180_to_190_miles'];
break;
case ( (floatval($distance_value_complete) >= 190) && (floatval($distance_value_complete) < 200) ):
$vehicle_price1 = $fixed_rate['from_190_to_200_miles'];
break;
case ( (floatval($distance_value_complete) >= 200) && (floatval($distance_value_complete) < 210) ):
$vehicle_price1 = $fixed_rate['from_200_to_210_miles'];
break;
case ( (floatval($distance_value_complete) >= 210) && (floatval($distance_value_complete) < 220) ):
$vehicle_price1 = $fixed_rate['from_210_to_220_miles'];
break;
case ( (floatval($distance_value_complete) >= 220) && (floatval($distance_value_complete) < 230) ):
$vehicle_price1 = $fixed_rate['from_220_to_230_miles'];
break;
case ( (floatval($distance_value_complete) >= 230) && (floatval($distance_value_complete) < 240) ):
$vehicle_price1 = $fixed_rate['from_230_to_240_miles'];
break;
case ( (floatval($distance_value_complete) >= 240) && (floatval($distance_value_complete) < 250) ):
$vehicle_price1 = $fixed_rate['from_240_to_250_miles'];
break;
case ( (floatval($distance_value_complete) >= 250) && (floatval($distance_value_complete) < 260) ):
$vehicle_price1 = $fixed_rate['from_250_to_260_miles'];
break;
case ( (floatval($distance_value_complete) >= 260) && (floatval($distance_value_complete) < 270) ):
$vehicle_price1 = $fixed_rate['from_260_to_270_miles'];
break;
case ( (floatval($distance_value_complete) >= 270) && (floatval($distance_value_complete) < 280) ):
$vehicle_price1 = $fixed_rate['from_270_to_280_miles'];
break;
case ( (floatval($distance_value_complete) >= 280) && (floatval($distance_value_complete) < 290) ):
$vehicle_price1 = $fixed_rate['from_280_to_290_miles'];
break;
case ( (floatval($distance_value_complete) >= 290) && (floatval($distance_value_complete) < 300) ):
$vehicle_price1 = $fixed_rate['from_290_to_300_miles'];
break;
}
else:
switch (true) {
case floatval($distance_value_complete) >= 300:
$vehicle_price1 = floatval($distance_value_complete) * $variable_rate['from_300_and_above'];
break;
}
endif;
return $vehicle_price1;
}
Simply rather not use a switch in such cases, where you have to deal with complex comparison expressions:
<?php
function calculatePrice($distance_value_complete, $fixed_rate, $variable_rate)
{
$distance_completed = floatval($distance_value_complete);
static $minimum_distance = 2;
static $maximum_distance = 300;
if ($distance_completed >= $maximum_distance) {
return $distance_completed * $variable_rate['from_' . $maximum_distance . '_and_above'];;
}
if ($distance_completed < $minimum_distance) {
return $fixed_rate['upto_' . $minimum_distance . '_mile'];
}
static $fixed_distances = array(
4, 9, 14, 19, 25, 29, 35, 40, 45, 50, 56, 62, 70, 80, 90, 100,
110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
210, 220, 230, 240, 250, 260, 270, 280, 290, 300
);
$last_distance = $minimum_distance;
foreach ($fixed_distances as $distance) {
if ($distance_completed < $distance) {
return $fixed_rate['from_' . $last_distance . '_to_' . $distance . '_miles'];
}
$last_distance = $distance;
}
// This should never happen, unless you modify $fixed_distances and forget to change the early return conditions
throw new \Exception(sprintf('Distance %f out of range', $distance_completed));
}
Test results:
0 = upto_2_mile
1 = upto_2_mile
2 = from_2_to_4_miles
3 = from_2_to_4_miles
4 = from_4_to_9_miles
5 = from_4_to_9_miles
6 = from_4_to_9_miles
7 = from_4_to_9_miles
8 = from_4_to_9_miles
9 = from_9_to_14_miles
10 = from_9_to_14_miles
11 = from_9_to_14_miles
12 = from_9_to_14_miles
13 = from_9_to_14_miles
14 = from_14_to_19_miles
15 = from_14_to_19_miles
16 = from_14_to_19_miles
17 = from_14_to_19_miles
18 = from_14_to_19_miles
19 = from_19_to_25_miles
20 = from_19_to_25_miles
21 = from_19_to_25_miles
22 = from_19_to_25_miles
23 = from_19_to_25_miles
24 = from_19_to_25_miles
25 = from_25_to_29_miles
26 = from_25_to_29_miles
27 = from_25_to_29_miles
28 = from_25_to_29_miles
29 = from_29_to_35_miles
30 = from_29_to_35_miles
31 = from_29_to_35_miles
32 = from_29_to_35_miles
33 = from_29_to_35_miles
34 = from_29_to_35_miles
35 = from_35_to_40_miles
36 = from_35_to_40_miles
37 = from_35_to_40_miles
38 = from_35_to_40_miles
39 = from_35_to_40_miles
40 = from_40_to_45_miles
41 = from_40_to_45_miles
42 = from_40_to_45_miles
43 = from_40_to_45_miles
44 = from_40_to_45_miles
45 = from_45_to_50_miles
46 = from_45_to_50_miles
47 = from_45_to_50_miles
48 = from_45_to_50_miles
49 = from_45_to_50_miles
50 = from_50_to_56_miles
51 = from_50_to_56_miles
52 = from_50_to_56_miles
53 = from_50_to_56_miles
54 = from_50_to_56_miles
55 = from_50_to_56_miles
56 = from_56_to_62_miles
57 = from_56_to_62_miles
58 = from_56_to_62_miles
59 = from_56_to_62_miles
60 = from_56_to_62_miles
61 = from_56_to_62_miles
62 = from_62_to_70_miles
63 = from_62_to_70_miles
64 = from_62_to_70_miles
65 = from_62_to_70_miles
66 = from_62_to_70_miles
67 = from_62_to_70_miles
68 = from_62_to_70_miles
69 = from_62_to_70_miles
70 = from_70_to_80_miles
71 = from_70_to_80_miles
72 = from_70_to_80_miles
73 = from_70_to_80_miles
74 = from_70_to_80_miles
75 = from_70_to_80_miles
76 = from_70_to_80_miles
77 = from_70_to_80_miles
78 = from_70_to_80_miles
79 = from_70_to_80_miles
80 = from_80_to_90_miles
81 = from_80_to_90_miles
82 = from_80_to_90_miles
83 = from_80_to_90_miles
84 = from_80_to_90_miles
85 = from_80_to_90_miles
86 = from_80_to_90_miles
87 = from_80_to_90_miles
88 = from_80_to_90_miles
89 = from_80_to_90_miles
90 = from_90_to_100_miles
91 = from_90_to_100_miles
92 = from_90_to_100_miles
93 = from_90_to_100_miles
94 = from_90_to_100_miles
95 = from_90_to_100_miles
96 = from_90_to_100_miles
97 = from_90_to_100_miles
98 = from_90_to_100_miles
99 = from_90_to_100_miles
100 = from_100_to_110_miles
101 = from_100_to_110_miles
102 = from_100_to_110_miles
103 = from_100_to_110_miles
104 = from_100_to_110_miles
105 = from_100_to_110_miles
106 = from_100_to_110_miles
107 = from_100_to_110_miles
108 = from_100_to_110_miles
109 = from_100_to_110_miles
110 = from_110_to_120_miles
111 = from_110_to_120_miles
112 = from_110_to_120_miles
113 = from_110_to_120_miles
114 = from_110_to_120_miles
115 = from_110_to_120_miles
116 = from_110_to_120_miles
117 = from_110_to_120_miles
118 = from_110_to_120_miles
119 = from_110_to_120_miles
120 = from_120_to_130_miles
121 = from_120_to_130_miles
122 = from_120_to_130_miles
123 = from_120_to_130_miles
124 = from_120_to_130_miles
125 = from_120_to_130_miles
126 = from_120_to_130_miles
127 = from_120_to_130_miles
128 = from_120_to_130_miles
129 = from_120_to_130_miles
130 = from_130_to_140_miles
131 = from_130_to_140_miles
132 = from_130_to_140_miles
133 = from_130_to_140_miles
134 = from_130_to_140_miles
135 = from_130_to_140_miles
136 = from_130_to_140_miles
137 = from_130_to_140_miles
138 = from_130_to_140_miles
139 = from_130_to_140_miles
140 = from_140_to_150_miles
141 = from_140_to_150_miles
142 = from_140_to_150_miles
143 = from_140_to_150_miles
144 = from_140_to_150_miles
145 = from_140_to_150_miles
146 = from_140_to_150_miles
147 = from_140_to_150_miles
148 = from_140_to_150_miles
149 = from_140_to_150_miles
150 = from_150_to_160_miles
151 = from_150_to_160_miles
152 = from_150_to_160_miles
153 = from_150_to_160_miles
154 = from_150_to_160_miles
155 = from_150_to_160_miles
156 = from_150_to_160_miles
157 = from_150_to_160_miles
158 = from_150_to_160_miles
159 = from_150_to_160_miles
160 = from_160_to_170_miles
161 = from_160_to_170_miles
162 = from_160_to_170_miles
163 = from_160_to_170_miles
164 = from_160_to_170_miles
165 = from_160_to_170_miles
166 = from_160_to_170_miles
167 = from_160_to_170_miles
168 = from_160_to_170_miles
169 = from_160_to_170_miles
170 = from_170_to_180_miles
171 = from_170_to_180_miles
172 = from_170_to_180_miles
173 = from_170_to_180_miles
174 = from_170_to_180_miles
175 = from_170_to_180_miles
176 = from_170_to_180_miles
177 = from_170_to_180_miles
178 = from_170_to_180_miles
179 = from_170_to_180_miles
180 = from_180_to_190_miles
181 = from_180_to_190_miles
182 = from_180_to_190_miles
183 = from_180_to_190_miles
184 = from_180_to_190_miles
185 = from_180_to_190_miles
186 = from_180_to_190_miles
187 = from_180_to_190_miles
188 = from_180_to_190_miles
189 = from_180_to_190_miles
190 = from_190_to_200_miles
191 = from_190_to_200_miles
192 = from_190_to_200_miles
193 = from_190_to_200_miles
194 = from_190_to_200_miles
195 = from_190_to_200_miles
196 = from_190_to_200_miles
197 = from_190_to_200_miles
198 = from_190_to_200_miles
199 = from_190_to_200_miles
200 = from_200_to_210_miles
201 = from_200_to_210_miles
202 = from_200_to_210_miles
203 = from_200_to_210_miles
204 = from_200_to_210_miles
205 = from_200_to_210_miles
206 = from_200_to_210_miles
207 = from_200_to_210_miles
208 = from_200_to_210_miles
209 = from_200_to_210_miles
210 = from_210_to_220_miles
211 = from_210_to_220_miles
212 = from_210_to_220_miles
213 = from_210_to_220_miles
214 = from_210_to_220_miles
215 = from_210_to_220_miles
216 = from_210_to_220_miles
217 = from_210_to_220_miles
218 = from_210_to_220_miles
219 = from_210_to_220_miles
220 = from_220_to_230_miles
221 = from_220_to_230_miles
222 = from_220_to_230_miles
223 = from_220_to_230_miles
224 = from_220_to_230_miles
225 = from_220_to_230_miles
226 = from_220_to_230_miles
227 = from_220_to_230_miles
228 = from_220_to_230_miles
229 = from_220_to_230_miles
230 = from_230_to_240_miles
231 = from_230_to_240_miles
232 = from_230_to_240_miles
233 = from_230_to_240_miles
234 = from_230_to_240_miles
235 = from_230_to_240_miles
236 = from_230_to_240_miles
237 = from_230_to_240_miles
238 = from_230_to_240_miles
239 = from_230_to_240_miles
240 = from_240_to_250_miles
241 = from_240_to_250_miles
242 = from_240_to_250_miles
243 = from_240_to_250_miles
244 = from_240_to_250_miles
245 = from_240_to_250_miles
246 = from_240_to_250_miles
247 = from_240_to_250_miles
248 = from_240_to_250_miles
249 = from_240_to_250_miles
250 = from_250_to_260_miles
251 = from_250_to_260_miles
252 = from_250_to_260_miles
253 = from_250_to_260_miles
254 = from_250_to_260_miles
255 = from_250_to_260_miles
256 = from_250_to_260_miles
257 = from_250_to_260_miles
258 = from_250_to_260_miles
259 = from_250_to_260_miles
260 = from_260_to_270_miles
261 = from_260_to_270_miles
262 = from_260_to_270_miles
263 = from_260_to_270_miles
264 = from_260_to_270_miles
265 = from_260_to_270_miles
266 = from_260_to_270_miles
267 = from_260_to_270_miles
268 = from_260_to_270_miles
269 = from_260_to_270_miles
270 = from_270_to_280_miles
271 = from_270_to_280_miles
272 = from_270_to_280_miles
273 = from_270_to_280_miles
274 = from_270_to_280_miles
275 = from_270_to_280_miles
276 = from_270_to_280_miles
277 = from_270_to_280_miles
278 = from_270_to_280_miles
279 = from_270_to_280_miles
280 = from_280_to_290_miles
281 = from_280_to_290_miles
282 = from_280_to_290_miles
283 = from_280_to_290_miles
284 = from_280_to_290_miles
285 = from_280_to_290_miles
286 = from_280_to_290_miles
287 = from_280_to_290_miles
288 = from_280_to_290_miles
289 = from_280_to_290_miles
290 = from_290_to_300_miles
291 = from_290_to_300_miles
292 = from_290_to_300_miles
293 = from_290_to_300_miles
294 = from_290_to_300_miles
295 = from_290_to_300_miles
296 = from_290_to_300_miles
297 = from_290_to_300_miles
298 = from_290_to_300_miles
299 = from_290_to_300_miles
300 = from_300_and_above
301 = from_300_and_above
302 = from_300_and_above
303 = from_300_and_above
304 = from_300_and_above
305 = from_300_and_above
306 = from_300_and_above
307 = from_300_and_above
308 = from_300_and_above
309 = from_300_and_above
310 = from_300_and_above
311 = from_300_and_above
312 = from_300_and_above
313 = from_300_and_above
314 = from_300_and_above
315 = from_300_and_above
316 = from_300_and_above
317 = from_300_and_above
318 = from_300_and_above
319 = from_300_and_above
320 = from_300_and_above
321 = from_300_and_above
322 = from_300_and_above
323 = from_300_and_above
324 = from_300_and_above
325 = from_300_and_above
326 = from_300_and_above
327 = from_300_and_above
328 = from_300_and_above
329 = from_300_and_above
330 = from_300_and_above
331 = from_300_and_above
332 = from_300_and_above
333 = from_300_and_above
334 = from_300_and_above
335 = from_300_and_above
336 = from_300_and_above
337 = from_300_and_above
338 = from_300_and_above
339 = from_300_and_above
340 = from_300_and_above
341 = from_300_and_above
342 = from_300_and_above
343 = from_300_and_above
344 = from_300_and_above
345 = from_300_and_above
346 = from_300_and_above
347 = from_300_and_above
348 = from_300_and_above
349 = from_300_and_above
350 = from_300_and_above```
Since you have a rate name depending on a single int value I would create an array that maps all the available rates, then pass it to a function with the value you are checking, something like this ( map is not complete, php7.2 required )
$conditionMap = [
[
'value' => 'upto_2_mile',
'condition' => '<2'
],
[
'value' => 'from_2_to_4_miles',
'condition' => '<4'
],
[
'value' => 'from_4_to_9_miles',
'condition' => '<9'
],
[
'value' => 'from_9_to_14_miles',
'condition' => '<14'
],
[
'value' => 'from_14_to_19_miles',
'condition' => '<19'
],
[
'value' => 'from_19_to_25_miles',
'condition' => '<25'
],
[
'value' => 'from_25_to_29_miles',
'condition' => '<29'
]
];
function getRate(int $value, array $conditionMap) : string {
$rate = 'Not found';
foreach ($conditionMap as $condition) {
$phpString = sprintf("return %s %s;", $value, $condition['condition']);
$evaluation = eval($phpString);
if($evaluation) {
$rate = $condition['value'];
break;
}
}
return $rate;
}
// TEST
for($i = 0; $i<=30; $i++) {
var_dump(sprintf("With value %s: %s", $i, getRate($i, $conditionMap)));
}
If you run this script you should obtain the result below
string(25) "With value 0: upto_2_mile"
string(25) "With value 1: upto_2_mile"
string(31) "With value 2: from_2_to_4_miles"
string(31) "With value 3: from_2_to_4_miles"
string(31) "With value 4: from_4_to_9_miles"
string(31) "With value 5: from_4_to_9_miles"
string(31) "With value 6: from_4_to_9_miles"
string(31) "With value 7: from_4_to_9_miles"
string(31) "With value 8: from_4_to_9_miles"
string(32) "With value 9: from_9_to_14_miles"
string(33) "With value 10: from_9_to_14_miles"
string(33) "With value 11: from_9_to_14_miles"
string(33) "With value 12: from_9_to_14_miles"
string(33) "With value 13: from_9_to_14_miles"
string(34) "With value 14: from_14_to_19_miles"
string(34) "With value 15: from_14_to_19_miles"
string(34) "With value 16: from_14_to_19_miles"
string(34) "With value 17: from_14_to_19_miles"
string(34) "With value 18: from_14_to_19_miles"
string(34) "With value 19: from_19_to_25_miles"
string(34) "With value 20: from_19_to_25_miles"
string(34) "With value 21: from_19_to_25_miles"
string(34) "With value 22: from_19_to_25_miles"
string(34) "With value 23: from_19_to_25_miles"
string(34) "With value 24: from_19_to_25_miles"
string(34) "With value 25: from_25_to_29_miles"
string(34) "With value 26: from_25_to_29_miles"
string(34) "With value 27: from_25_to_29_miles"
string(34) "With value 28: from_25_to_29_miles"
string(24) "With value 29: Not found"
string(24) "With value 30: Not found"
I have a string of numerical values separated with spaces:
70 58 81 909 70 215 70 1022 580 930 898 70 276 31 11 **920 898** 1503 195 770 573 508 1015 31 8 815 1478 31 1022 31 1506 31 **318 500 358 865** 358 991 518 58 450 420 487 31 1478 108 70 1022 31 215 318 500 61 31 655 1061 918 54 898 31 8 1011 9 8 459 346 770 751 31 346 770 880 1171 688 1680 31 1002 769 500 61 8 702 898 8 1206 31 709 565 8 138 58 215 81 1171 31 288 500 380 70 284 1500 565 31 601 55 1501 490 565 530 56 990 380 1061 770 345 1171 31 55 1100 605 1471 1234 **31 470 725 358 114 56 9 55** 1100 1610 1471 1000 971 565 55 1100 1610 1061 770 345 949 31 370 52 688 1680 770 880 1171 163 249 151 489 653 56 990 380 503 490 770 1376 1056 31 8 64 490 565 55 108 56 1178 501 653 898 860 565 31 315 61 509 108 501 653 31 349 249 151 489 246 56 990 380 1070 573 1663 725 821 31 70 373 1171 490 565 55 108...
I want to get all occurrences of the string, where combined sum of numbers is x. So, if I search: 2041 it should return an array ("318 500 358 865") or if I search: 1818 it should return an array ("920 898", "31 470 725 358 114 56 9 55").
I also need solution to be optimal, because string of numbers can sum up to hundreds of thousands.
Principle explained in theory would be good, but preferred languages are PHP or NodeJs if solution is given by programming language. Even MySQL solution would be nice, if possible. But solution by any language would be nice anyway.
Extra notes
numbers are all positive integers
number values are between 1-10000
Usage:
$ex = new finder($string);
$result = $ex->search(979)->find(true); // true = Hard mode
First we pass the string we want to search, then we use method to_array() so we can create the array from the string, then you need to choose which mode you want to use.
It has 2 modes, soft search and hard search.
you can choose which one to use in the find method, ->find(true) , True = hard mode, False = soft mode;
The soft mode it is really simple, creates a new multidimensional array with keys as the strlen of the numbers (method order()), so if we are searching for a number 70 we don't really wanna use the 3 digits or more, i mean ( 100, 99999 etc... ). Then it just minus the search value with the number and tries to search in the entire array with (in_array) if it has the result;
foreach ($this->_clean as $clean) {
$minus = abs($clean - $search);
// Simple and fast query
if(in_array($minus, $this->_clean)){
$tmp[] = array($minus,$clean);
}
}
The hard mode is just as the name states, we will loop through every number.
public function hard_search($array, $partial){
$s = 0;
foreach ($partial as $x){
$s += $x;
}
if ($s == $this->_search){
$this->_tmp[] = $partial;
}
if ($s < $this->_search){
for($i=0;$i<count($array);$i++) {
$remaining = array();
$n = $array[$i];
for ($j=$i+1; $j<count($array); $j++) {
array_push($remaining, $array[$j]);
};
$partial_rec = $partial;
array_push($partial_rec, $n);
$this->hard_search($remaining,$partial_rec);
}
}
return $this->_tmp;
}
Of course we could really make a few more tweaks but from what i tested it gives good results.
Feel free to ask if you have any questions.
Example with outputs: http://codepad.viper-7.com/INvSNo
class finder {
public $stop;
protected $_string,
$_search,
$_tmp,
$_array = array(),
$_array_order = array(),
$_clean = array();
public function __construct($string){
$this->_string = $string;
}
/**
* String to array
*
* #return \find
*/
public function to_array(){
$this->_array = array_keys(
array_flip(
explode(' ', $this->_string)
)
);
return $this;
}
/**
* what we are searching for
*
* #param string/int $search
* #return \finder
*/
public function search($search){
$this->reset();
$this->_search = $search;
$this->to_array();
return $this;
}
/**
*
* #param type $a // array
* #param type $total // total
* #return array
*/
public function find($hard = false){
$this->_hard = $hard;
if(is_array($this->_search)){
foreach($this->_search as $search){
$result[] = $this->search_array($search);
}
}else{
$result = $this->search_array($this->_search);
}
return $result;
}
/**********************************
* SOFT SEARCH
***********************************/
/**
* Multidimensional Array with strlen as the key
*
* #return \find
*/
public function order(){
// Order
foreach($this->_array as $n){
$this->_array_order[strlen($n)][] = $n;
}
return $this;
}
public function clean($search){
$tmp = array();
$check_length = function($number) use($search){
if($number <= $search){
return $number;
}
return false;
};
foreach(range(key($this->_array_order), strlen($search)) as $v){
$tmp = array_map($check_length,array_merge($tmp, $this->_array_order[$v]));
}
$this->_clean = array_filter($tmp);
sort($this->_clean);
return $this;
}
public function search_array($search) {
$res = array();
if($this->_hard == false){
$this->order();
$this->clean($search);
$res = $this->soft_search($search);
}else{
$res = $this->hard_search($this->_array, array());
}
return $res;
}
/**
*
* #return array
*/
public function soft_search($search){
$tmp = array();
foreach ($this->_clean as $clean) {
$minus = abs($clean - $search);
// Simple and fast query
if(in_array($minus, $this->_clean)){
$tmp[] = array($minus,$clean);
}
}
return $tmp;
}
/**********************************
* END SOFT SEARCH
***********************************/
public function hard_search($array, $partial){
$s = 0;
foreach ($partial as $x){
$s += $x;
}
if ($s == $this->_search){
$this->_tmp[] = $partial;
}
// if higher STOP
if ($s < $this->_search){
for($i=0;$i<count($array);$i++) {
$remaining = array();
$n = $array[$i];
for ($j=$i+1; $j<count($array); $j++) {
array_push($remaining, $array[$j]);
};
$partial_rec = $partial;
array_push($partial_rec, $n);
$this->hard_search($remaining,$partial_rec);
}
}
return $this->_tmp;
}
/****************************
*
* Extra
*
*****************************/
public function reset(){
$this->_search = '';
$this->_clean = array();
$this->_array = array();
$this->_array_order = array();
$this->_tmp = array();
$this->_tmp = array();
return $this;
}
public function new_string($string){
$this->reset();
$this->_string = $string;
$this->to_array();
return $this;
}
}
$string = '70 58 81 909 70 215 130 70 1022 580 930 898 70 276 31 11 920 898 1503 195 770 573 508 '
. '1171 490 565 55 108';
$ex = new finder($string);
echo '<pre>';
echo '<h1>Hard 979</h1>';
$result = $ex->search(979)->find(true);
print_r($result);
echo '<h1>Soft 979</h1>';
$result = $ex->search(979)->find();
print_r($result);
echo '<h1>Hard 238</h1>';
$result = $ex->search(238)->find(true);
print_r($result);
echo '<h1>Soft 238</h1>';
$result = $ex->search(238)->find();
print_r($result);
Output example for 285:
Hard 285
Array
(
[0] => Array
(
[0] => 70
[1] => 215
)
[1] => Array
(
[0] => 58
[1] => 130
[2] => 31
[3] => 11
[4] => 55
)
)
Soft 285
Array
(
[0] => Array
(
[0] => 215
[1] => 70
)
)
Here you go, in Python. This has linear complexity:
def list2string( alist ):
return " ".join( map(str, alist ))
def string2list( s ):
return list(map( int, s.split() ))
def find_number( a, total ):
u = 0
y = 0 # the current sum of the interval (u .. v)
res = []
for v in range( 0, len(a) ):
# at this point the interval sum y is smaller than the requested total,
# so we move the right end of the interval forward
y += a[v]
while y >= total:
if y == total:
res.append( list2string( a[ u : v+1 ] ) )
# if the current sum is too large, move the left end of the interval forward
y -= a[u]
u += 1
return res
text = "70 58 81 909 70 215 70 1022 580 930 898"
alist = string2list(text)
print( find_number( alist, 285) )
I'm assuming there are no negative values in your list, and the list consists purely of integers separated by spaces. You shouldn't have any problems translating it into any other language. I hope the algorithm is self-explanatory, if not ask.
I need to echo only selected lines from a .txt file using PHP.
The txt file content looks like this:
1 111 111 111 111 111 111 111 111 111 111
2 196 182 227 190 195 196 278 197 323 265
3 84.1 84.1 84.1 84.2 85.4 84.1 84.2 84.1 84.1 84.1
4 107 107 107 107 107 107 107 107 107 107
5 10.0 9.29 9.86 9.90 9.57 9.56 9.52 9.55 10.0 9.62
6 0.652 0.622 0.676 0.617 0.637 0.617 0.563 0.601 0.586 0.601
7 132 132 132 132 132 132 481 132 132 132
8 113 113 113 113 113 113 113 113 113 113
9 510 571 604 670 647 667 656 257 264 431
10 245 246 247 246 246 245 247 246 247 247
The previous working code was echoing every lines.
$fp = fopen("http://xxx/myfile.txt","r");
while (!feof($fp)) {
$page .= fgets($fp, 4096);
}
fclose($fp);
echo $page;
break;
I'm trying to get the same formating but only with the selected lines.
For instance let say only lines: 3, 5, 8, 10.
The second step would then be if the lines could be echoed in a different order than the initial one.
If someone knows a simple way to do it, that would be excellent!
Thanx in advance.
$lines = file('http://xxx/myfile.txt');
print $lines[2]; // line 3
print $lines[4]; // line 5
...
see file()
here's an example of how it can be done:
function echo_selected_lines_of_file($file, array $lines)
{
$fContents = file_get_contents($file); // read the file
$fLines = explode("\n", $fContents); // split the lines
foreach($fLines as $key=>$fLine) // foreach line...
if(in_array($key, $lines)) //if its in the $lines array, print it
echo $fLine.'<br>';
}
echo_selected_lines_of_file('index.php', array(1,4,7));
You could use the file() which converts each line into a array. But if your file is big it will consume a bit of memory.
$lines_to_print = [1,3,5,8];
$lines = file('file.txt');
for($i in $line_to_print) {
echo $lines[$i];
}
An efficient solution is
$i = 0;
$lines_to_print = [1,3,5,8];
$file = fopen("file.txt", "r");
while(!feof($file)){
$line = fgets($file);
if(in_array(++$i, $lines_to_print)) {
echo $line
}
}
fclose($file);