I try to write a file on my server, but I don't understand what's happening : it's always empty (blank page).
There is no error thrown.
When I insert inside the loop a var_dump($lines), I see the data but at the moment an error appear
With the code below
$minLat = -3.0000; //41.34343606848294;
$maxLat = 22.0000; //57.844750992891;
$minLng = -0.0300; //-16.040039062500004;
$maxLng = 90.4200; //29.311523437500004;
$step = 0.1;
$k = 1;
$estimator = new KNearestNeighbors(9);
$estimator->train($dataset->getSamples(), $dataset->getTargets());
$lines = [];
for($lat=$minLat; $lat<$maxLat; $lat+=$step) {
for($lng=$minLng; $lng<$maxLng; $lng+=$step) {
$lines[] = sprintf('%s;%s;%s', $lat, $lng, $estimator->predict([[$lat, $lng]])[0]);
}
}
var_dump($lines); ==> display info, but always empty
$filename = '/var/www/test/php-ml/result_map.csv';
//$content = implode(PHP_EOL, $lines);
$content = implode( "\n", $lines );
file_put_contents($filename, $content);
the result (example)
-------------
bool(true) ==> check if the file exist
array(1) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" }
array(2) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" [1]=> string(40) "-3;0.07;Apple 15 Inch MacBook Pro Laptop" }
array(3) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" [1]=> string(40) "-3;0.07;Apple 15 Inch MacBook Pro Laptop" [2]=> string(40) "-3;0.17;Apple 15 Inch MacBook Pro Laptop" }
array(4) { [0]=> string(41) "-3;-0.03;Apple 15 Inch MacBook Pro Laptop" [1]=> string(40) "-3;0.07;Apple 15 Inch MacBook Pro Laptop" [2]=> string(40) "-3;0.17;Apple 15 Inch MacBook Pro Laptop" [3]=> string(40) "-3;0.27;Apple 15 Inch MacBook Pro Laptop" }
Related
I upgraded a Joomla site php from 5.* to 7.1.1 and now I get this warning:
A non well formed numeric value encountered in
Here is the part of the code:
foreach (JProfiler::getInstance('Application')->getMarks() as $mark)
{
var_dump($mark);
$totalTime += $mark->time; //Error line here
$totalMem += (float) $mark->memory;
var_dump output:
object(stdClass)#12 (6) { ["prefix"]=> string(11) "Application"
["time"]=> string(16) "+75.977802276611" ["totalTime"]=>
float(75,977802276611) ["memory"]=> string(16) "+1.3794021606445"
["totalMemory"]=> float(1,7153472900391) ["label"]=> string(9)
"afterLoad" }
So if I change $totalTime += $mark->time; to $totalTime += strtotime($mark->time);
I get this error:
Warning: Division by zero in ...
Which line is:
'width' => round($mark->time / ($totalTime / 100), 4),
I am trying to create a project which will help students study various areas. The idea is that I have a piece of raw text, which contains quiz questions and answers which I want to parse as question header and answer options, which will be inserted into a database. However, the text is not properly formatted and due to the large amount of questions and answers (around ~20k per total), I cannot afford the time to manually insert them or format the text myself.
The raw text looks like this:
1. A car averages 27 miles per gallon. If gas costs $4.04 per gallon, which of the following is closest to how much the gas would cost for this car to travel 2,727 typical miles?
a) $44.44 b) $109.08 c) $118.80
d) $408.04 e)
$444.40
2. When x = 3 and y = 5, by how much does the value of 3x2 – 2y exceed the value of 2x2 – 3y ?
a) 4
b) 14
c) 16
d) 20 e) 50
I tried creating my own PHP functions to parse the text properly, however I cannot get myself to get past the random line breaks, spaces, etc.
What I am trying to obtain:
array(1) {
[0]=>
array(3) {
["questionNumber"]=>
string(1) "1"
["questionText"]=>
string(175) "A car averages 27 miles per gallon. If gas costs $4.04 per gallon, which of the following is closest to how much the gas would cost for this car to travel 2,727 typical miles?"
["options"]=>
array(5) {
["a"]=>
string(6) "$44.44"
["b"]=>
string(7) "$109.08"
["c"]=>
string(7) "$118.80"
["d"]=>
string(7) "$408.04"
["e"]=>
string(7) "$444.40"
}
}
}
The code I have so far:
$rawText = '1. A car averages 27 miles per gallon. If gas costs $4.04 per gallon, which of the following is closest to how much the gas would cost for this car to travel 2,727 typical miles?
a) $44.44 b) $109.08 c) $118.80
d) $408.04 e)
$444.40
2. When x = 3 and y = 5, by how much does the value of 3x2 – 2y exceed the value of 2x2 – 3y ?
a) 4
b) 14
c) 16
d) 20 e) 50
';
$rawTextLines = explode("\n", $rawText);
foreach ($rawTextLines as $lineNumber => $lineContents) {
$lContents = trim($lineContents);
if (empty ($lContents)) {
unset ($rawTextLines[$lineNumber]);
} else {
$rawTextLines[$lineNumber] = $lContents;
}
}
$processedQuestions = array ();
$currentQuestionHeader = 0;
foreach ($rawTextLines as $lineNumber => $lineContents) {
if (ctype_digit(substr($lineContents, 0, 1))) { // Question header
$questionHeaderInformation = explode('.', $lineContents);
$currentQuestionHeader = $questionHeaderInformation[0];
$processedQuestions[$currentQuestionHeader]['questionNumber'] = $currentQuestionHeader;
$processedQuestions[$currentQuestionHeader]['questionText'] = $questionHeaderInformation[1];
} else { // Question option
$options = explode(')', $lineContents);
if (count ($options) % 2 === 0) {
$processedQuestions[$currentQuestionHeader]['options'][trim($options[0])] = ucfirst(trim($options[1]));
} else {
}
}
}
Which produces this:
array(2) {
[1]=>
array(3) {
["questionNumber"]=>
string(1) "1"
["questionText"]=>
string(35) " A car averages 27 miles per gallon"
["options"]=>
array(1) {
["a"]=>
string(8) "$44.44 b"
}
}
[2]=>
array(3) {
["questionNumber"]=>
string(1) "2"
["questionText"]=>
string(96) " When x = 3 and y = 5, by how much does the value of 3x2 – 2y exceed the value of 2x2 – 3y ?"
["options"]=>
array(3) {
["a"]=>
string(1) "4"
["b"]=>
string(2) "14"
["c"]=>
string(2) "16"
}
}
}
As you can see, the current output does not match - not by far, what I am trying to obtain.
Thank you in advance.
Hellow,
^[0-9]+\. (.*)[\r\n]+a\)[\s]+(.*)[\s]+b\)[\s]+(.*)[\s]+c\)[\s]+(.*)[\s]+d\)[\s]+(.*)[\s]+e\)[\s]+(.*)[\s]*
Try it !
$re = '/^[0-9]+\. (.*)[\r\n]+a\)[\s]+(.*)[\s]+b\)[\s]+(.*)[\s]+c\)[\s]+(.*) [\s]+d\)[\s]+(.*)[\s]+e\)[\s]+(.*)[\s]*/m';
$str = '1. A car averages 27 miles per gallon. If gas costs $4.04 per gallon, which of the following is closest to how much the gas would cost for this car to travel 2,727 typical miles?
a) $44.44 b) $109.08 c) $118.80
d) $408.04 e)
$444.40
2. When x = 3 and y = 5, by how much does the value of 3x2 – 2y exceed the value of 2x2 – 3y ?
a) 4
b) 14
c) 16
d) 20 e) 50';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
I want to ping a range of ips on the server that runs Unix
exec('ping -c 4 '.$ip, $output, $return_var);
Problem is that, it doesn't properly ping the ips that actually gets pinged but can't be found on ns_lookup in cmd. what I mean is that some of these ips gets pinged and some not. On localhost, that runs windows, it works fine.
this is the $output array I get when I ping that ip on server (runs Unix):
array(5) {
[0]=>
string(54) "PING 91.208.144.2 (91.208.144.2) 56(84) bytes of data."
[1]=>
string(0) ""
[2]=>
string(36) "--- 91.208.144.2 ping statistics ---"
[3]=>
string(64) "4 packets transmitted, 0 received, 100% packet loss, time 2999ms"
[4]=>
string(0) ""
}
that's the $output array I get when I ping it on localhost (on Windows):
array(11) {
[0]=>
string(0) ""
[1]=>
string(43) "Pinging 91.208.144.2 with 32 bytes of data:"
[2]=>
string(50) "Reply from 91.208.144.2: bytes=32 time=9ms TTL=248"
[3]=>
string(51) "Reply from 91.208.144.2: bytes=32 time=10ms TTL=248"
[4]=>
string(51) "Reply from 91.208.144.2: bytes=32 time=11ms TTL=248"
[5]=>
string(50) "Reply from 91.208.144.2: bytes=32 time=8ms TTL=248"
[6]=>
string(0) ""
[7]=>
string(33) "Ping statistics for 91.208.144.2:"
[8]=>
string(56) " Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"
[9]=>
string(46) "Approximate round trip times in milli-seconds:"
[10]=>
string(48) " Minimum = 8ms, Maximum = 11ms, Average = 9ms"
}
exec and ping is available on server cuz it works perfect with the ips that gets pinged and can be looked up in cmd.
Any ideas ?
If i execute var_dump(array($output)) is shows
array(1) { [0]=> string(573) " __(Developer’s Must Have Gray Cardigan|:89.77,gravityforms), __(Polka Dots Blue Dress|:55.45,gravityforms), __(Classic Brown Leather Bag with Orange Details|:66.84,gravityforms), __(Cycling Pack Steel Blue|:360.00,gravityforms), __(Classic Brown Leather Bag with Orange Details|:,gravityforms), __(The Black Cat Winter Jacket|:254.45,gravityforms), __(Split Slit Gold Threading Cardi|:165.74,gravityforms), __(Slim Fit Pants|:85,gravityforms), __(Get the Complete WordPress Developer Outfit|:65.55,gravityforms), __(Biodiesel Cardigan Dreamcatcher|:175.00,gravityforms)," }
But i want to like this
array(10) { [0]=> string(44) "Developer’s Must Have Gray Cardigan|:89.77" [1]=> string(28) "Polka Dots Blue Dress|:55.45" [2]=> string(52) "Classic Brown Leather Bag with Orange Details|:66.84" [3]=> string(31) "Cycling Pack Steel Blue|:360.00" [4]=> string(47) "Classic Brown Leather Bag with Orange Details|:" [5]=> string(35) "The Black Cat Winter Jacket|:254.45" [6]=> string(39) "Split Slit Gold Threading Cardi|:165.74" [7]=> string(18) "Slim Fit Pants|:85" [8]=> string(50) "Get the Complete WordPress Developer Outfit|:65.55" [9]=> string(39) "Biodiesel Cardigan Dreamcatcher|:175.00" }
how can i do this?
Use array_push
$outputArr = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$price = get_post_meta(get_the_ID(), '_regular_price', true);
array_push($outputArr, ' __(' . get_the_title() . '|:' . $price . ',gravityforms)');
}
}
var_dump($outputArr);
Hope this helps
If you want use this array further make foreach and execute
Code
$l = array(
" __(Developer’s Must Have Gray Cardigan|:89.77,gravityforms), __(Polka Dots Blue Dress|:55.45,gravityforms), __(Classic Brown Leather Bag with Orange Details|:66.84,gravityforms), __(Cycling Pack Steel Blue|:360.00,gravityforms), __(Classic Brown Leather Bag with Orange Details|:,gravityforms), __(The Black Cat Winter Jacket|:254.45,gravityforms), __(Split Slit Gold Threading Cardi|:165.74,gravityforms), __(Slim Fit Pants|:85,gravityforms), __(Get the Complete WordPress Developer Outfit|:65.55,gravityforms), __(Biodiesel Cardigan Dreamcatcher|:175.00,gravityforms),"
);
$m = explode('__', $l[0]);
$n = array();
array_walk($m, function ($x, $y) use (&$n) {
$w = trim(preg_replace('/,(|\s+)$/', '', $x));
$n[] = ($w) ? '__'.$w : '';
});
$result = array_slice(array_filter($n), 0);
var_dump($result);
Output
Edit
Or you can just use this
$l = array(
" __(Developer’s Must Have Gray Cardigan|:89.77,gravityforms), __(Polka Dots Blue Dress|:55.45,gravityforms), __(Classic Brown Leather Bag with Orange Details|:66.84,gravityforms), __(Cycling Pack Steel Blue|:360.00,gravityforms), __(Classic Brown Leather Bag with Orange Details|:,gravityforms), __(The Black Cat Winter Jacket|:254.45,gravityforms), __(Split Slit Gold Threading Cardi|:165.74,gravityforms), __(Slim Fit Pants|:85,gravityforms), __(Get the Complete WordPress Developer Outfit|:65.55,gravityforms), __(Biodiesel Cardigan Dreamcatcher|:175.00,gravityforms),"
);
$n = explode(', ', $l[0]);
This question already has answers here:
Getting N random numbers whose sum is M
(9 answers)
Closed 1 year ago.
do you know a way to split an integer into say... 5 groups.
Each group total must be at random but the total of them must equal a fixed number.
for example I have "100" I wanna split this number into
1- 20
2- 3
3- 34
4- 15
5- 18
EDIT: i forgot to say that yes a balance would be a good thing.I suppose this could be done by making a if statement blocking any number above 30 instance.
I have a slightly different approach to some of the answers here. I create a loose percentage based on the number of items you want to sum, and then plus or minus 10% on a random basis.
I then do this n-1 times (n is total of iterations), so you have a remainder. The remainder is then the last number, which isn't itself truley random, but it's based off other random numbers.
Works pretty well.
/**
* Calculate n random numbers that sum y.
* Function calculates a percentage based on the number
* required, gives a random number around that number, then
* deducts the rest from the total for the final number.
* Final number cannot be truely random, as it's a fixed total,
* but it will appear random, as it's based on other random
* values.
*
* #author Mike Griffiths
* #return Array
*/
private function _random_numbers_sum($num_numbers=3, $total=500)
{
$numbers = [];
$loose_pcc = $total / $num_numbers;
for($i = 1; $i < $num_numbers; $i++) {
// Random number +/- 10%
$ten_pcc = $loose_pcc * 0.1;
$rand_num = mt_rand( ($loose_pcc - $ten_pcc), ($loose_pcc + $ten_pcc) );
$numbers[] = $rand_num;
}
// $numbers now contains 1 less number than it should do, sum
// all the numbers and use the difference as final number.
$numbers_total = array_sum($numbers);
$numbers[] = $total - $numbers_total;
return $numbers;
}
This:
$random = $this->_random_numbers_sum();
echo 'Total: '. array_sum($random) ."\n";
print_r($random);
Outputs:
Total: 500
Array
(
[0] => 167
[1] => 164
[2] => 169
)
Pick 4 random numbers, each around an average of 20 (with distribution of e.g. around 40% of 20, i.e. 8). Add a fifth number such that the total is 100.
In response to several other answers here, in fact the last number cannot be random, because the sum is fixed. As an explanation, in below image, there are only 4 points (smaller ticks) that can be randomly choosen, represented accumulatively with each adding a random number around the mean of all (total/n, 20) to have a sum of 100. The result is 5 spacings, representing the 5 random numbers you are looking for.
Depending on how random you need it to be and how resource rich is the environment you plan to run the script, you might try the following approach.
<?php
set_time_limit(10);
$number_of_groups = 5;
$sum_to = 100;
$groups = array();
$group = 0;
while(array_sum($groups) != $sum_to)
{
$groups[$group] = mt_rand(0, $sum_to/mt_rand(1,5));
if(++$group == $number_of_groups)
{
$group = 0;
}
}
The example of generated result, will look something like this. Pretty random.
[root#server ~]# php /var/www/dev/test.php
array(5) {
[0]=>
int(11)
[1]=>
int(2)
[2]=>
int(13)
[3]=>
int(9)
[4]=>
int(65)
}
[root#server ~]# php /var/www/dev/test.php
array(5) {
[0]=>
int(9)
[1]=>
int(29)
[2]=>
int(21)
[3]=>
int(27)
[4]=>
int(14)
}
[root#server ~]# php /var/www/dev/test.php
array(5) {
[0]=>
int(18)
[1]=>
int(26)
[2]=>
int(2)
[3]=>
int(5)
[4]=>
int(49)
}
[root#server ~]# php /var/www/dev/test.php
array(5) {
[0]=>
int(20)
[1]=>
int(25)
[2]=>
int(27)
[3]=>
int(26)
[4]=>
int(2)
}
[root#server ~]# php /var/www/dev/test.php
array(5) {
[0]=>
int(9)
[1]=>
int(18)
[2]=>
int(56)
[3]=>
int(12)
[4]=>
int(5)
}
[root#server ~]# php /var/www/dev/test.php
array(5) {
[0]=>
int(0)
[1]=>
int(50)
[2]=>
int(25)
[3]=>
int(17)
[4]=>
int(8)
}
[root#server ~]# php /var/www/dev/test.php
array(5) {
[0]=>
int(17)
[1]=>
int(43)
[2]=>
int(20)
[3]=>
int(3)
[4]=>
int(17)
}
$number = 100;
$numbers = array();
$iteration = 0;
while($number > 0 && $iteration < 5) {
$sub_number = rand(1,$number);
if (in_array($sub_number, $numbers)) {
continue;
}
$iteration++;
$number -= $sub_number;
$numbers[] = $sub_number;
}
if ($number != 0) {
$numbers[] = $number;
}
print_r($numbers);
This should do what you need:
<?php
$tot = 100;
$groups = 5;
$numbers = array();
for($i = 1; $i < $groups; $i++) {
$num = rand(1, $tot-($groups-$i));
$tot -= $num;
$numbers[] = $num;
}
$numbers[] = $tot;
It won't give you a truly balanced distribution, though, since the first numbers will on average be larger.
I think the trick to this is to keep setting the ceiling for your random # generator to 100 - currentTotal
The solution depends on how random you want your values to be, in other words, what random situation you're going to simulate.
To get totally random distribution, you'll have to do 100 polls in which each element will be binded to a group, in symbolic language
foreach i from 1 to n
group[ random(1,n) ] ++;
For bigger numbers, you could increase the selected group by random(1, n/100) or something like that until the total sum would match the n.
However, you want to get the balance, so I think the best for you would be the normal distribution. Draw 5 gaussian values, which will divide the number (their sum) into 5 parts. Now you need to scale this parts so that their sum would be n and round them, so you got your 5 groups.
The solution I found to this problem is a little different but makes makes more sense to me, so in this example I generate an array of numbers that add up to 960. Hope this is helpful.
// the range of the array
$arry = range(1, 999, 1);
// howmany numbers do you want
$nrresult = 3;
do {
//select three numbers from the array
$arry_rand = array_rand ( $arry, $nrresult );
$arry_fin = array_sum($arry_rand);
// dont stop till they sum 960
} while ( $arry_fin != 960 );
//to see the results
foreach ($arry_rand as $aryid) {
echo $arryid . '+ ';
}