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
I have string like this : hello world new foo.
I need to have array like this:
[
`helloworld new foo`,
`hello worldnew foo`,
`hello world newfoo`,
`helloworldnew foo`,
`hello worldnewfoo`,
]
order doesn't matter but i need to have all case that space will be remove in current string.
You can use recursion to enumerate all the posibilities:
<?php
function combine($words, $acc=array(), $res=array()){
if(count($words)===0){
$res[] = join("", $acc);
return $res;
}
if(count($words)===1){
$res[] = join("", $acc).$words[0];
return $res;
}
$w1 = array_shift($words);
$res = combine($words, array_merge($acc, array("$w1")), $res);
$res = combine($words, array_merge($acc, array("$w1 ")), $res);
return $res;
}
var_dump( combine( explode(' ', 'hello world new foo') ) );
Another possible solution is to represent the N spaces betweeen your words, as bits in a binary number that can be on or off, and then count from 0 to 2^N-1, but I think that in PHP that will be more complex.
NB: the above recursive solution, returns all possible combinations ... so if you have an input array with 4 words, one of the results will have all 4 words joined.
From what i understood you need all posible combinations with the words given. so:
posible combinations = amountofwords*amountofspaces.
start iteration for amout of posible cominations. -> for(i=0;i<=(words*spaces);i++)
have words in array and spaces found in String so $WordArray = $string.split(" ") and $spaces = substr_count(" ")
start iteration for posible word combinations. for(j=0;j<=words;j++)
start iteration for amount of spaces. for(k=0;k<=spaces;++)
combine all.
but keep in mind that PERMUTATIONS and COMBINATIONS for computer science is what you need to learn first, so the answer above has a lot of sence.
here is a link to get you started.
http://cpsc.ualr.edu/srini/DM/chapters/review3.4.html
Related
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 4 years ago.
Improve this question
how can i using php divide every single value in an array on the next value to return a number like a have an array
i want a function that divide 1/2/3/4/5 and it should return this value (0.0083333333333333)
i have tried this
<?php
function vo(){
$newArray=array(1,2,3,4,5);
$resulte=1;
foreach($newArray as $value){
$resulte=$value/$resulte;
}}
vo();
?>
output for this code, resulte=1.875
i think im bad at math not sure tho
$your_array = [1,2,3,4,5];
$result = $your_array[0]; //result defaulted to first element in array;
for($i=1; $i<sizeof($your_array); $i++){ //loop starts from 2nd //element in array
$result /= $your_array[$i];
}
code explanation: set initial value of $result to first element of array, as this is the first thing that you said you want to divide;
start looping through your array from 2nd element, and set the value of $result to it's current value divided by the current element in the loop (this is why /= is used)
the final value of $result, becomes the accumulated result of the division of each element of the array divided by the element next to it.
if you output $result, you will get 0.0083333333333333
You got your division backwards. You want to divide by the next number in the list, therefore your $resulte = $value / $resulte should be $resulte = $resulte / $value. Also remember to add a print($resulte) after the loop.
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 7 years ago.
Improve this question
An array has negative and positive numbers. Separate the numbers such that negative numbers are at the beginning and positive numbers at the end without changing the order.
Example:
Array = {1, -3, -5, 9 , -8}
O/P = {-3, -5, -8, 1, 9}
I found many answer in c , c++ , java but not in PHP , so can any one please let me know how or better way to achieve this?
However this question put on hold , i tried with some of solutions and
find my own answer added below , hope it might be useful for someone.
function part($arr){
$j = 0;
for($i=0;$i<count($arr);$i++){
$val = $arr[$i];
$k = $i;
while($k>$j && $val < 0){
$arr[$k] = $arr[$k-1];
$k = $k-1;
if($j==$k){
$j=$j+1;
}
$arr[$k] = $val;
}
}
return $arr;
}
$arr = array(1, -3, -5, 9 , -8);
print_r(part($arr));
Without doing all the work for you.
Look into array_filter you could filter the negative values into 1 array, then filter the positive values into another array.
Then use array_merge to merge the 2.
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
I am trying to calculate two variables type "String" in php.
and I try all alternative but steady amount missing values.
example:
$a = "250,000.50";
$b = "30,000.00";
echo $a - $b;
I want to return value with same format ###,###,###.## -String -Decimal -only return same format.
Note: I try all the link format currency here...use regex, sprintf, replace, etc...
but always lose a digit because the example use in format en_US or EUR.
Thanks!.
You can use this function for easily handle:
$a = "250,000.50";
$b = "30,000.00";
function currency($c=0){
return preg_replace("/[^\d\.]+/iu","",$c);
}
echo number_format(currency($a) - currency($b) ,2);
The subtraction on strings will auto-cast the strings into numbers for you, but you have to get rid of the commas first:
$a = "250,000.50";
$b = "30,000.00";
echo str_replace(',', '', $a) - str_replace(',', '', $b);
Oh, and wrap the whole thing in number_format to get it back to the comma format:
echo number_format( str_replace(',', '', $a) - str_replace(',', '', $b), 2);
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
Right now i am working on a simple PHP script.
I have four variables:
$test_a = 25;
$test_b = 24;
$test_c = 22;
$test_d = 35;
I want to display not the numbers from the variables. I need to extract only the two variables with the highest number.
So i need result something like this:
<?PHP echo "The two highest variables are: $test_a and $test_d";?>
So how i can extract only the two highest variables?
Add the values to array, sort it in descending order, then take two first elements:
$a = array($test_a, $test_b, $test_c, $test_d);
arsort($a);
echo 'Two highest values:'.$a[0].' and '.$a[1];
Put your values in an array: $array = array(25, 24, 22, 35); and get the first highest value using php max function:
$highest[] = max($array); //store it in an array so you can compare using array_diff
Remove that value from your array w/ array_diff:
$array = array_diff($array, $highest); //remove highest from original array
And then repeat finding highest with max:
$second_highest = max($array);
echo "The two highest variables are: $highest[0] and $second_highest";
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
My code:
<?php
$string = "hello world";
echo $string[1]; //output is 'e'
echo end($string); // I've got a warning error
?>
How can I exchange my string variable to an array format?
I think if I exchange it the problem will solve.
$string[n] is a specific notation to access the nth offset of a string. This does not mean the string is an array, it's just special syntactic sugar. If you want the last offset of the string, use substr($string, -1).
There is a predefined function that do this. It's str_split(). Here http://it.php.net/manual/en/function.str-split.php you find the doc
Just do this:
echo substr($string, -1);
Because a string is not natively an array, functions like end and array_sort don't work.
To physically cast the string, use this:
function CastStringToArray($string)
{
$ret = array();
$length = strlen($string);
for ($i = 0; $i < $length; $i++)
{
$ret[] = $string[$i];
}
return $ret;
}