I've been looking high and low for this, but I can't seem to find the best way to do this?
I want to basically run through a simple range of numbers 1-20, and every time there is a "3" listed, like 3 or 13, replace that with a value like "thisisa3value"
I'm just a little stumped on the best way that can be done?
This is the code I have so far but it doesn't seem to be working, as it basically prints this out on each number. I want it to basically just ONLY do that for ones that have a 3 associated with it. Can someone please take a look and let me know what I'm doing wrong?
<?php
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ( in_array(3, range(1,20)) ) {
echo ' thisisa3value ';
}
}
?>
To reiterate, I just want it to basically print out like this:
1, 2, this is a 3 value, 4, 5, 6, 7, 8, 9, 10, 11, 12, this is a 3 value, 14, 15, 16, 17, 18, 29, 20.
Any help would be greatly appreciated. Thanks!
This Code may help you
foreach (range(1, 20) as $number ) {
echo $number;
echo '<br>';
if (strstr($number, '3')) {
echo ' thisisa3value <br>';
}
}
Based on your example, any appearance of a 3 is valid, such as 13.
$text = join(', ', range(1, 20));
echo preg_replace('/(\d*3\d*)/', 'this is a 3 value', $text);
Produces:
1, 2, this is a 3 value, 4, 5, 6, 7, 8, 9, 10, 11, 12, this is a 3 value, 14, 15, 16, 17, 18, 19, 20
Using loop to find the digit in that particular number
foreach (range(1, 20) as $number ) {
if( isDigitPresent($number, 3)){
echo 'thisisa3value ,';
}
else{
echo $number . ' ,' ;
}
}
function isDigitPresent( $number , $digit ){
$found = false;
while( $number > 0 ){
if( ($number % 10) == $digit ){
$found = true;
break;
}
$number = $number/10;
}
return $found ? $found : false;
}
Related
I need a simpler way like 1 - 100 instead of adding any number and number (1,2,3....)
$all_urls = array(1,2,3,4..,100);
foreach ($all_urls as $url){
$xml = simplexml_load_file("http://localhost/$url");
}
You can use range():
Create an array containing a range of elements
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
echo $number;
}
$all_urls = range(1,100);
First of all, thanks for looking at my question.
I only want to add up the positive numbers in the $numbers using a if,else statement.
$numbers = array (1, 8, 12, 7, 14, -13, 8, 1, -1, 14, 7);
$total = 0;
if ($numbers < 0 {
$numbers = 0;
}
elseif (now i want only the positive numbers to add up in the $total.)
I'm an first years student and I am trying to understand the logic.
I'm not gonna give the direct answer, but the way here is you need a simple loop, can be for or a foreach loop, so every iteration you just need to check whether the current number in the loop is grater than zero.
Example:
$numbers = array (1, 8, 12, 7, 14, -13, 8, 1, -1, 14, 7);
$total = 0;
foreach($numbers as $number) { // each loop, this `$number` will hold each number inside that array
if($number > 0) { // if its greater than zero, then make the arithmetic here inside the if block
// add them up here
// $total
} else {
// so if the number is less than zero, it will go to this block
}
}
Or as michael said in the comments, a function also can be used in this purpose:
$numbers = array (1, 8, 12, 7, 14, -13, 8, 1, -1, 14, 7);
$total = array_sum(array_filter($numbers, function ($num){
return $num > 0;
}));
echo $total;
$numbers = array (1, 8, 12, 7, 14, -13, 8, 1, -1, 14, 7);
$total = 0;
foreach($numbers as $number)
{
if($number > 0)
$total += $number;
}
this loops through all elements of the array(foreach = for each number in the array) and checks if the element is bigger than 0, if it is, add it to the $total
I have the following randomly generated string:
$text = 's$bp4q1hsq3#g88nsjm5hr#i9#3078e2m';
What I need is to take all integers from it and classify them as either prime or composite numbers and estimate their sum. All numbers should be assumed that are one digit so this shortens the values to only four per each group:
$primes = array(2, 3, 5, 7);
$composites = array(4, 6, 8, 9);
This means: Primes: 5, 3, 3, 2 = 13 and Composites: 4, 8, 8, 9, 8 = 37 as duplicate numbers also count.
I have tried grabbing the numbers like so:
$asArray = str_split($text);
foreach ($asArray as $element) {
if (is_int($element)) {
echo $element;
}
}
But it seems to end up in a blank page. So my question is how can I find out the numbers in a string and then classify them as either prime or composite?
Here you have the sum of the primes and composites:
$text = 's$bp4q1hsq3#g88nsjm5hr#i9#3078e2m';
$primes = array(2, 3, 5, 7);
$sum_primes = $sum_composites = 0;
preg_match_all("/\d/", $text, $matches);
foreach($matches[0] as $number)
{
if (in_array($number, $primes))
$sum_primes += $number;
else
$sum_composites += $number;
}
echo "Sum of primes: ".$sum_primes."\n";
echo "Sum of composites: ".$sum_composites."\n";
It would print,
Sum of primes: 20
Sum of composites: 38
i need help with a question, I need to write code in php that will
find the missing number in the sequence, regardless of that numbers position in the sequence.
the numbers will increase but the same amount each time.
the output must be only the number that was missing from the initial list, not just the list with the number in it (I have worked that out myself).
example number sequence, $sequence = 3, 5, 9, 11, 13
obviously the number 7 is missing, but I don't know how to do the code, im assuming it would use loops, but i wouldn't even know were to start, It must be in PHP
A more simple way to get the missing number(s) from the sequence, the way I see it:
<?php
$sequence = array(3, 5, 9, 11, 13);
$numbers = array(7, 9, 15);
$single_nr = 7;
function getMissingNumber(array $sequence, $single_nr = null, $numbers = array())
{
// Check if it exists in the sequence array
// check single number
if($single_nr)
{
if(!in_array($single_nr, $sequence))
{
echo $single_nr . " is not in array" . "<br />";
}
}
// check an array of numbers
if($numbers)
{
foreach($numbers as $nr)
{
if(!in_array($nr, $sequence))
{
echo $nr . " is not in array" . "<br />";
}
}
}
}
echo getMissingNumber($sequence, null, $numbers);
?>
Only uses the first three elements to find the interval (needs a 2 out of 3 match) and doesn't validate if the rest of the sequence follow the rule, but also finds more missing elements:
<?php
$sequence = array(3, 5, 9, 13, 15, 17, 21);
// guess
$step = $sequence[1] - $sequence[0];
// validate
if (($sequence[2] - $sequence[1]) != $step) {
if (($sequence[3] - $sequence[2]) != $step) {
$step = $sequence[2] - $sequence[1];
if (($sequence[3] - $sequence[2]) != $step) {
die('first three intervals are all different');
}
}
}
$should = range($sequence[0], $sequence[count($sequence) - 1], $step);
echo 'Missing: ', implode(', ', array_diff($should, $sequence));
<?php
$str="3,5,9,11,13" ;
$arr=explode(',', $str);
$no=array();
for ($i=3; $i <30 ; $i=$i+2) {
$m=$i+2;
if (!(in_array($m, $arr)))
{
$no[]=$m;
}
}
print_r($no);
// OUTPUT- Array ( [0] => 7 [1] => 15 )
?>
I want generate a range of numbers orderly. I mean opposite of rand function in php. I test with range but it generate only array of numbers
For example
min 1 and max 3
rand(1,3);// 2,1,3
but i want 1,2,3
Any in built in functions in php like rand for getting this features without using any loops?
Use range() to generate numbers
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
echo $number;
}
You can achieve this using for loop
for ($i = 1; $i <= 3; $i++) {
echo $i;
}
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
echo $number;
}
If you are using php then take a look at range function
If I understand your slightly missleading question.. this might do it:
$string = implode(', ', range(1,3));
echo $string;
And not a loop in sight :)
Or as a one liner:
echo implode(', ', range(1,3));