how to get data from an array inside a recursive array? - php

Say I create a recursive array with this code:
$digits = 0;
$tens = 0;
$hundreds = 0;
for($i = 0; $i <= 100; $i++)
{
$myArray[$hundreds][$tens][$digits] = $i;
$digits++;
if($digits > 9)
{
$digits = 0;
$tens++;
}
if($tens > 9)
{
$tens = 0;
$hundreds++;
}
}
how could I echo out all the data fromt the 'tens array' == 2?
To be clear, I'd be looking for these results:
20 21 22 23 24 25 26 27 28 29
since im using base 10, I could just do this:
for($i=0; $i < 10; $i++)
{
echo $myArray[0][2][$i]
}
but what if i have no idea how many elements are in the digits array?

foreach($myArray[0][2] as $v) {
echo $v."<br>\n";
}

<?php
$tensArr = $myArray[0][2];
for($i= 0 ; $i < count($tensArr); $i++)
{
echo $tensArr[$i]."\n" ;
}

Related

How can received and clear an array in PHP

I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);

How to store each iteration of a for loop in an array? (PHP)

The code below echos out the multiples of 3 & 5 below 20. Please how can I store these values in an array as the loop iterates?
for ($i = 0; $i < 20; $i++) {
if ($i % 3 == 0 || $i % 5 == 0 ) {
$mul = $i;
echo "{$mul}, ";
Initialize the array. With PHP 5.4, you can do $arr = []; whereas with earlier versions of PHP, you have to do $arr = array();.
Run array_push($arr, $mul) where $arr is the array and $mul is the value
Without further ado, the code:
<?php
$result = [];
for ($i = 0; $i < 20; $i++) {
if ($i % 3 == 0 || $i % 5 == 0 ) {
array_push($result, $i);
}
}
print_r($result);
?>

How to execute an action of 10 in 10 interaction PHP

I have an array and I would like to take action on every iteration
for($i = 0; $i < count($array); $i++) {
0,1,3...10
// Execute
$create->save();
11, 12, 13... 20
// Execute
$create->save();
}
You can use the modulo of 10.
for($i = 0; $i < count($array); $i++) {
if($i%10 == 0{
$create->save();
}
}
Something like this?
$n = 10;
for($i = 0; $i < count($array); $i++) {
if($n == $i) {
$create->save();
$n = $n + 10;
}
}
But here in your loops 10 != 10, because is 11 loop. If you need to execute at every 10 loop, then $n = 9 or $i = 1;

continously loop with adding different value

I'm new in PHP. How can i achieve continously loop with adding different value?
it's something like this
<?php
$gap1 = 2;
$gap2 = 3;
$lenght = 10;
for( $i=0; $i<$length; $i++ )
{
//the code
}
?>
and the result will be : 0 2 5 7 10 12 15 17
thank you for your help :)
$gap1=2;
$gap2=3;
$lenght = 10;
$p=0;
for($i=0;$i<$lenght;$i++)
{
if($i==0){$p=0;}
elseif($i%2==0)
{
$p+=$gap2;
}
else{
$p+=$gap1;
}
echo $p.'<br>';
}
Try this code:
$gap1 = 2;
$gap2 = 3;
$length = 10;$i=0;
$x = 0;
while($i<$length)
{
echo $x." ";
if($i%2 == 0)
$x+=$gap1;
else
$x+=$gap2;
$i++;
}
Output:
0 2 5 7 10 12 15 17 20 22
$gap = array(2, 3);
$result = array(-1 => 0);
$length = 10;
for($i = 0; $i < $length; $i++) {
$result[] = $result[$i-1] + $gap[($i) % count($gap)];
}
echo implode(' ', $result);

Simple PHP for loop error

Just started learning. Here's what I have:
<?php
$i = 0;
$num = $i * 12;
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$num."<br>");
}
?>
The outcome should be:
1 times 12 = 12
2 times 12 = 24
3 times 12 = 36
etc...
The outcome I actually get is:
1 times 12 = 0
2 times 12 = 0
3 times 12 = 0
any ideas?
It is because you have this declaration before for loop:
$i = 0; $num = $i * 12;
so always $num will be 0. Just place it into for:
for ($i=1; $i<13; $i++) {
$num = $i*12;
echo($i." times 12 = ".$num."<br>");
}
You don't need declare $i variable before for loop. This variable will be overwritten. There is simple test:
$i = 5;
for($i = 1; $i<10; $i++);
echo $i;
OUTPUT:
10
<?php
$i = 1; $num = 1;
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
If you want that result you should put the calculation inside the for loop and start i with 1
<?php
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Something like this
<?php
$num = 12;
for ($i=1; $i<13; $i++) {
echo("$i times 12 = ".$num*$i);
echo "<br>";
}
?>
<?php
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$i*12."<br>");
}
?>
Line $num = $i * 12; shift into loop
<?php
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Your $num variable only ever increments on $i when its 0, try putting it in the for loop like this.
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}

Categories