Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to filter how many level I will add with my standard deviation result.
These are steps that I need to do:
Calculate the standard deviation based on array value. (solve)
Find the average of the array value and add with the standard deviation result.
eg : average = -49.7
s.d = 3.37
So, i need to keep adding the value until I get a new list of number.
eg: -49.7, -46.33, -42.96, -39.59
After that, I need to filter only print the array value from -49.7 to -39.59 only.
Can anybody help me on this?
Here is the script from what I could understand the question.
If you need any explanation or the code is not what you want then let me know in the comment section.
<?php
$array = [2,4,6,8,10];
$resultList = "";
$resultList_rounded = "";
function standardDeviation($array){
//sum of all values
$sum = array_sum($array);
//sum of (square of values)
$sum_of_square = array_sum(array_map(function($value){return $value*$value;},$array));
//X Bar
$xBar = average($array);
//variance
$variance = ($sum_of_square/count($array))-pow($xBar,2);
//standard deviation
return sqrt($variance);
}
function average($array){
return (array_sum($array)/count($array));
}
function newList($array,$rounded=false,$round_digits = 4){
$newarray = [];
$sd = standardDeviation($array);
$avg = average($array);
for($i=1;$i<=count($array);$i++){
if(empty($newarray)){
array_push($newarray,$avg);
continue;
}
if(!$rounded){
array_push($newarray,$array[$i-1]+$sd);
}else{
array_push($newarray,number_format((float) ($array[$i-1]+$sd), $round_digits, '.', ''));
}
}
return implode(',',$newarray);
}
function getRange($array){
return $array[0]." to ".$array[count($array)-1];
}
//get new list
$resultList = newList($array,true,0);
/*In the line above this, replace false with true if you want the rounded version and false if you want non rounded version. 4 means the number of digits to round.
examples:
$resultList = newList($array,true,4); will print 6,6.8284,8.8284,10.8284,12.8284
$resultList = newList($array,false,4); will print 6,6.8284271247462,8.8284271247462,10.828427124746,12.828427124746
$resultList = newList($array,true,0); will print 6,7,9,11,13
$resultList = newList($array,true,1); will print 6,6.8,8.8,10.8,12.8
*/
//print the new result list
echo $resultList;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Want to divide random integer number into equal five parts. And insert those values into array. Can any one tell me the logic for that.
Eg. I have 15 as my number. After dividing. It should generate the array as below.
$myArray = array('3','6','9','12','15');
Thanks in advance.
function getParts($number, $parts)
{
return array_map('round', array_slice(range(0, $number, $number / $parts), 1));
}
print_r(getParts(15, 5));
Explanation: range() generates the array of values starting with 0, ending when it reaches $number and using the step $number/$parts. It will get $parts+1 floating point numbers. array_slice() removes the first item (which is always 0). array_map() applies the function round() to each element to get the nearest integer.
Divide and create a loop to fill the array...
$total = 15;
$divide = 5;
$base = $total / $divide;
$arr = array();
for($i = 1; $i <= $divide; $i++) {
$arr[] = round($i * $base);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how do i insert a comma in string like
$str = "0470 06102009 2981485GIR ADE TAUHID";
every length i put in array like
$legth = array(7,8,15,50);
i just wanna make the result like
0470 ,06102009 ,2981485GIR ,ADE TAUHID
where every string splited according to length on the array, including whitespace,
length(7),length(8),length(15),length(50)
how i do that ?
I would do something like this:
$offset = 0;
$result = implode(",",array_map(function($length) use ($str,&$offset) {
$part = substr($str,$offset,$length);
$offset += $length;
return $part;
},$legth));
Demo: http://ideone.com/ISWZuO
Please note that the output of the demo is what you "should" get based on the input you provided. Your input is wrong for the output you say you want - adjust $legth as needed.
if you are sure of the number of spaces and you want to keep them, you can use substr
// substr( your string, start, length )
substr($str , 0, 7)
This will work for you:
$oldString = "0470 06102009 2981485GIR ADE TAUHID";
$newstring = implode(", ", preg_split("/[\s]+/", $oldString));
echo $newstring;
EDIT:
If you need output on the basis of your array. Then I think you need to modify your array first. And apply below code:
$legth = array(7,8,15,50);
$str = "0470 06102009 2981485GIR ADE TAUHID";
$first = 0;
foreach($legth as $l){
echo substr($str , $first, $l)."<br />";
$first = $first + $l;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I think the subject hits it better than I was expecting.
I need a function which returns random numbers which result in a given value (X) when they are summed up.
Something like this:
getRandomTo(10); // result for example: array(2,3,5)
getRandomTo(10); // result for example: array(4,3,3)
getRandomTo(12); // result for example: array(5,1,6)
I could not find a generic algorithm function for solving that requirement. Further more I cannot imagine a FAST and performant way to create something like this my self.
Please help me out
function getRandomTo($num)
{
$x = Array();
$i = 0;
do
{
$x[$i] = rand(1,$num);
$num = $num - $x[$i];
$i++;
}while($num > 0);
print_r($x);
}
Maybe do this:
create a random value between 0 and X; say this one is called r1. save this in your array.
create another random value between 0 and (X-r1), name it r2. save it also.
do these steps as often as you need it (or as long as r1+...+rn is lower than X)
Another solution:
function randomTo($numIn) {
$numOut = 0;
$numbers = array();
do {
$add = rand(1, $numIn);
if($numOut + $add > $numIn)
continue;
$numOut += $add;
$numbers[] = $add;
} while( $numOut != $numIn );
return $numbers;
}
$result = randomTo(15);
var_dump($result);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a simple form and a submit button. After pressing the submit button, get the values (they will be numbers) and calculate the sum. And again calculate the sum of individual digits stored in sum.
A better explanation: 1+2+3+4=10 where 1,2,3 and 4 are user inputs from the form. And the sum 10 has to be split-ted and summed again as 1+0=1. And this would be my final result. So far I did the task where it gives me the first sum. I don't know what to do to display the second result which I want to be the finale.
<?php
$a='';
$sum='';
$total='';
if (!isset($_POST['submit'])) {
echo " ";
}
else {
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
for ($i=0; $i<=12; $i++) {
$sum= array_sum($a);
$total= ;
}
}
echo "sbora e: $total ";
?>
$total = array_sum(str_split($sum));
Using Artefacto's method.
On another note,
$a = array($_POST['textfield'],$_POST['textfield1'],$_POST['textfield2'],$_POST['textfield3'],$_POST['textfield4'],$_POST['textfield5'],$_POST['textfield6'],$_POST['textfield7'],$_POST['textfield8'],$_POST['textfield9'],$_POST['textfield10'],$_POST['textfield11'],);
can be written as,
$a = array();
for ($i = 0; $i <= 11; $i++){
if (isset($_POST["textfield_$i"]))
array_push($a, $_POST["textfield_$i"]);
}
if the names of the fields are:
textfield_0, textfield_1, textfield_2...
So you want to build the cross sum of your first result:
$result2 = 0;
//cast integer to string
$strTotal = (string) $total;
//loop over "chars" and add them to your second result
for($i=0; $i < strlen($strTotal); $i++)
{
$result2 += $strTotal{$i};
}