for loop - print values in order - PHP [closed] - php

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 6 years ago.
Improve this question
I need to print the below statement.
Thanks for updating your PH_NUMBER, ADDRESS, OFFICE NUMBER and EMAIL.
Where PH_NUMBER, ADDRESS, OFFICE_NUMBER and EMAIL are variables.
For example, if value is empty for variable "OFFICE_NUMBER" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and EMAIL.
For example, if value is empty for variable "EMAIL" the sentence should be like below:
Thanks for updating your PH_NUMBER, ADDRESS and OFFICE NUMBER.
$a = "Address";
$b = "Mobile";
$c = "email";
$array = array($a, $b, $c);
$length = count($array);
for ($i = 0; $i < $length; $i++) {
$total = $array[$i];
}
echo "Thanks for udpating " . $total ;
How can this be done ?
Any suggestions would be helpful.

You don't need to use a loop.
<?php
/* Collect all parameters to the array */
$variables = [
'PH_NUMBER',
'ADDRESS',
'EMAIL',
'OFFICE_NUMBER',
];
/* Sorting the array if need */
/* Pop the element off the end of array */
$variables = array_filter($variables);
$count = count($variables);
$lastVariable = array_pop($variables);
if($count > 1) {
printf('Thanks for updating your %s and %s', implode(', ', $variables), $lastVariable);
}elseif($count === 1){
printf('Thanks for updating your %s', $lastVariable);
}else{
print 'Nothing for update';
}
?>
Update: Small improvement in order to catch cases with only 1 variable and empty array (nothing for update).

Related

Filter Standard Deviation using PHP [closed]

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;

Permutation in order algorithm if anyone could help me with the loop coding [closed]

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 6 years ago.
Improve this question
Hi there I was wondering if I could create an array that accept a sentence and turns it into an array for example
$sentence = 'my name is john'
then the array will be:
$arr = {my name is john,
my name is, my name john, my is john, name is john,
my name, my is, my john, name is, name john, is john,
my, name, is, john}
anyone could help me with the implementation in any kind of loop that would be great because im currently creating a simple search engine algorithm thx :D
Think of it as a n-bit integer, with each bit corresponding to whether a word is included or not in a given string in your array. Loop from 1 to (1 << n) - 1, which in this case is 1 to 15, to get your 15 word lists, and for each one, check each of the bits in the integer and add the corresponding word if the corresponding bit is set:
function getCombinations($sentence)
{
$words = explode(" ", $sentence);
$combinations = array();
for($i = 1; $i < (1 << count($words)); $i++)
{
$wordlist = "";
for($j = 0; $j < count($words); $j++)
{
if($i & (1 << $j))
{
$wordlist = $wordlist . " " . $words[$j];
}
}
array_push($combinations, substr($wordlist, 1));
}
return $combinations;
}
$a = "my name is john";
print_r(getCombinations($a));
If you want your strings to be sorted by the number of words, add an extra loop for the word counts:
for($wordcount = count($words); $wordcount >= 1; $wordcount--)
{
for($i = 1; $i < (1 << count($words)); $i++)
{
if(NumberOfSetBits($i) == $wordcount)
{
$wordlist = "";
// generate word list..
}
}
}
NumbeOfSetBits function from here

How To Display Random Results in PHP [closed]

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);

How to sum a set of values from a form in PHP? [closed]

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};
}

Append a number to every php instance [closed]

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 form in my website where users can send messages to multiple recipients, they type in the recipient's numbers separated by commas, I recieve the numbers in my php script as a single post variable, but I need to add the recipient,s area code on front of every number, I have tried to explode the variable using the comma as a separator but I do not know what to do from there
$recipient=$_POST['recipient'];
$numbers=explode(',' $recipient);
for ($i = 0; $i <=sizeof($numbers); $i++) {
}
I do not know how you're getting the area codes in the script, but to prepend the number with the area code, you can just use string concatenation, like so:
$areacode = 123; // this is received dynamically
$result = array(); // initialize empty array
for ($i = 0; $i <= sizeof($numbers); $i++) {
$result[] = $areacode . '_' . $numbers[$i];
}
This can also be done using a foreach:
foreach (explode(',', $recipient) as $number) {
$result[] = $areacode . '_' . $number;
}
Now, if you want to get this back as a comma-separated string, you can use implode(), like so:
$result_string = implode(',', $result);
The short answer is something like:
$numbers_with_area_codes = Array();
for ($i = 0; $i <= sizeof($numbers); $i++) {
$numbers_with_area_codes[] = '+372'.$numbers[$i];
}

Categories