Displaying All Array Contents - php

I have problem assigning values to the $word as array so that I can make it as a variable.
I am displaying an output of
1,000,000,000 they have all different echos; I want to make them as 1 value only example:
$output = $arramt[1]="1",$arramt[2]="000",$arramt[3]="000",$arramt[4]="000"
function mnyFmt($word){
$n = strlen($word);
if ($n%3==0){
$i = $n/3;
$x=0;
while($x<$i-1){
echo substr($word,-$n,3).",";
$arramt[$x] = substr($word,-$n,3).",";
$x++;
$n = $n-3;
}
echo substr($word,-$n,3);
$arramt[$x] = substr($word,-$n,3).",";
}
elseif($n%3==1){
$word1 = substr($word,1,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,1).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$arramt[$x] = substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
$arramt[$x] = substr($word1,-$w1,3).",";
echo array_values($arramt[$x]);
}
elseif($n%3==2){
$word1 = substr($word,2,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,2).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
}
}
$amount = 1000000000;
mnyFmt($amount);
}
Thanks very much in advance.

You don't need to make any of the custom function you can simply use number_format and explode function like as
$amount = 1000000000;
print_r(explode(',',number_format($amount)));
Demo

Related

I want to echo out a total number per repeat

I have created a small script which gives me random numbers. Now I want to create a list of this random number list with repeating numbers.
It should generate match numbers of this list of random numbers (A match number is a number which repeats)
For instance 4 – 6 – 9 – 32 – 34 – 31 – 5 – 32 (The match number here is 8 because after 8 numbers we have a repeater). I would like all the match numbers to be echo beside each other with a space in between.
Can someone help me?
I have tried creating an if statement but I can't get it working.
for ($rnd=1;$rnd<=50;$rnd++)
{
$random = mt_rand(0,100) . " ";
echo $random;
}
Explanation coming soon.
$numbers = array();
for($i = 1; $i <= 50; $i++) {
$number = mt_rand(0,100);
if(!isset($numbers[$number])) $numbers[$number] = array();
$numbers[$number][] = $i;
}
foreach($numbers as $key => $value) {
$start = '';
foreach($value as $k => $v) {
echo $start . $key . ' (Match Number: ' . $v . ')';
$start = ' - ';
}
echo '<br />';
}
If I don't misunderstood your question then this is what you want. Let's do this way-
<?php
$existing = [];
$repeat_numbers = [];
for ($rnd=1;$rnd<=50;$rnd++)
{
$random = mt_rand(0,100);
if(in_array($random,$existing)){
$repeat_numbers[] = $rnd; // pushing the repeating index
}
$existing[] = $random;
echo $random.PHP_EOL;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/eEhB8
AS PER THE COMMENT
<?php
$existing = [];
$repeat_numbers = [];
for ($rnd=1;$rnd<=50;$rnd++)
{
$randoms[] = mt_rand(0,100);
}
echo implode('-',$randoms).PHP_EOL;
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
}
$existing[] = $rnd;
$i++;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO https://3v4l.org/Xjc5X
AS PER THE LATEST COMMENT
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','3-31-34-29-28-5-28-23-31-4-1-31-11-17-23-9-20-24-22-3-11-24-26-4-10');
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
$existing = []; // This like will do the magic for you
}
$existing[] = $rnd;
$i++;
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/hIT22
FINAL EDIT:
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','4-9-13-18-19-34-23-9-9-13-44-5-13-13-88-26-29-27-34-67-65-83-26');
$i = 1;
foreach($randoms as $rnd){
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
$i=1;
$existing = [];
}else{
$existing[] = $rnd;
$i++;
}
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/9j7iq
EDITED AGAIN:
<?php
$existing = [];
$repeat_numbers = [];
$randoms = explode('-','4-9-13-18-19-34-23-9-9-13-44-5-13-13-88-26-29-27-34-67-65-83-26');
//print_r($randoms);
$i = 0;
foreach($randoms as $rnd){
$i++;
if(in_array($rnd,$existing)){
$repeat_numbers[] = $i;
if(count($existing)==1){
$i=1;
}else{
$i=0;
}
$existing = [];
}
$existing[] = $rnd;
//print_r($existing);
}
echo implode('-',$repeat_numbers);
?>
WORKING DEMO: https://3v4l.org/VfIJY

How to arrange the word of a given string in ascending oredr in php?

I want to get a proper solution for my coding.
Step 1. I first break it into array
Step 2. then split each explode value
Step 3. then sort it by a for loop
<?php
$st ="It is a test";
$exp = explode(' ',strtolower($st));
print_r($exp);
$c =0;
$p =0;
for($i=0;$i<count($exp);$i++){
$t = str_split($exp[$i]);
$ch = $t[0];
if($c==0){
$arr[$p]=$exp[$i];
$temp = $ch;
}else{
if(ord($ch)<ord($temp)){
$tp = $arr[$p-1];
$arr[$p-1] = $exp[$i];
$arr[$p] = $tp;
}else{
$arr[$p] = $exp[$i];
}
}
$temp = $ch;
$p++;
$c++;
}
print_r($arr);
?>
Any help would be appreciated.
Not sure what you want, but I think this is what you want.
<?php
$st ="It is a test";
$exp = explode(' ',strtolower($st));
sort($exp);
for($x = 0; $x <count($exp); $x++) {
echo $exp[$x];
echo "<br>";
}
?>

While loop not working with x <=num

$x = 1;
$num = 15;
while($x <= $num) {
$res = '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
}
echo $res;
That's my code but it only shows the following when I try it:
[TR][TD="align: left"]15[/TD][TD="align: left"][/TD][/TR]
It should show that but instead of only 15 it should be 1-15 inclusive.
Any ideas?
You keep setting $res to a new value rather than appending to it. Using $res .= 'something'; is like saying $res = $res . 'something';. Doing this will allow you to keep the previous value of $res and appending more to the end of it.
$x = 1;
$num = 15;
$res = '';
while($x <= $num) {
$res .= '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
}
echo $res;
$x = 1;
$num = 15;
while($x <= $num) {
$res = '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
echo $res;
}
Put echo $res; inside the loop, otherwise it will echo just the $res from the last loop cycle instead of all 15 times.

String as calculation in php

I've got field in my database which contain strings like 21;48;33;22;31. Then I'd like to convert it to mathematical calculation 21+48+33+22+31.
$points = "21;48;33;22;31";
$points = str_replace(";","+",$points );
$points = preg_replace('/[^0-9\+\-\*\/\s]+/', '', $points);
echo $points;
But that simply does not work. I've got the same string "21+48+33+22+31" instead of the sum.
$points = "21;48;33;22;31";
$points = explode(';',$points );
$points = array_sum($points);
echo $points;
$points = "21;48;33;22;31";
$arr = explode(";", $points);
$points = 0;
foreach($arr as $key => $rows){
$points += $rows[$key];
}
echo $points;
Try above code it will give you proper result.
or you can try it also:
$points = "21;48;33;22;31";
$arr = explode(";", $points);
echo $points = array_sum($arr);
The easiest way is to explode the string.
Then you can iterate with foreach over the resulting array and calculate them.
$points = "21;48;33;22;31";
$points = explode(";", $points);
$calc = 0;
forearch($points as $point) {
$calc += $point;
}
Or your can use array_sum:
$points = "21;48;33;22;31";
$points = explode(";", $points);
$calc = array_sum($points);
Something like that. Perhaps there are some shorter ways.
$string = "21;48;33;22;31";
$string = explode(";" , "21;48;33;22;31");
$point = 0;
foreach($string as $num)
{ // by using (int) you can convert string to int.
$point = (int)$num + $point;
}
print($point);
// output
155
explode it then loop for sum...
<?php
$total = 0; // for getting the total
$points = "21;48;33;22;31";
$points = explode(';',$points);
for($i=0;$i<count($points);$i++){
$total+= $points[$i];
}
echo $total;
?>
<?php
eval('$sum = ' . str_replace(';','+','21;48;33;22;31').';');
echo $sum;

How can I store for loop in a variable?

How can I store this in a $_var variable ?
$s_number = 5;
$spn = '6';
echo "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++)
{
echo "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
echo "]";
Question is bit vague though it seems probably you are looking for this,
$string = "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++) {
$string .= "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$string .= "]";
echo $string;
Other suggestions that use strings are fine, but I prefer to create arrays for tasks like this:
$s_number = 5;
$spn = '6';
$landscapes_array = array();
for ($i = 1; $i <= $s_number; $i++) {
$landscapes_array[] = "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$landscapes = "'Landscapes':[" . implode('', $landscapes_array) . "]";
You could also try putting the for loop in a function passing your variables and then return the values to a new variable. That's like saving a loop in a variable.

Categories