store variables in an mutidimensional array? - php

i tried to store variables which are set in a while loop in a multi dimensional arrays. Afterwarts i want to print the array out.
what i did:
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
}
When i run this code i will not get anything.
What is the reason for this?
Thank you!

Here is code-
<?php
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
$i++;
}

You need to add the index of the array you are adding to or you are just writing over it.
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[$counter] = array($a,$b);
$counter++;
}
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
if ($a != $b)
echo $a.' is not '.$b;
else
echo $a.'='.$b;
$i++;
}

Related

get value 1 - 2 - 1 - 2 - 1 in for loop php

I am trying to get value like 1-2-1-2-1-2 from for loop
I tried code like this
$c = count($response['video']); // 2
$currentPage = 2;
$k = 1;
for ($i=$k; $i <= $currentPage ; $i++) {
$b = $i > $c ? $k : $i;
$urls = $response['video'][$b - 1];
echo "$b";
}
where $currentPage = 5; $c =2;
when $c = 3; the value should be 1-2-3-1-2-3
when i echo $b or dd($b) out of loop i will got 12111 but i need answer 121212
please help me to solve this.
Is this what you want?
$k = 1;
$currentPage = 5;
$c = 3;
for ($b=$i=$k; $i <= $currentPage ; $i++, $b++) {
$b = $b <= $c ? $b : 1;
echo $b;
}

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

PHP add to variable name

Is it possible to add or concatenate something into a variable name in a PHP variable? For example:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$grade.[$g] = $grades[$i];
}
}
}
I want this to happen:
$grade7 = 0
$grade8 = 1
$grade9 = 2
$grade10 = 3
Concatenates the $g with grade and make this value a variable by adding a $ sign at the starting line...
The example given below:
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
${"grade".$g} = $grades[$i];
}
}
}
echo $grade7; // 0
echo $grade8; // 1
echo $grade9; // 2
echo $grade10;// 3
One solution would be to create the variables dynamically:
for ($g = 7; $g <= 10; $g++) {
for ($i = 0; $i <= 4; $i++) {
$counter = $g - 7;
if ($i == $counter) {
${'grade' . $g} = $grades[$i];
}
}
}
You should use ARRAY instead of your method. :)
Try to look at variable named like "$$".
EDIT: Maybe something like
var $array = array();
for($g=7; $g<=10; $g++)
{
for($i=0; $i<=4; $i++)
{
$counter = $g - 7;
if($i != $counter) continue;
else $array[$grade.[$g]] = $grades[$i];
}
}
$data = new Array();
for($g = 7; $g <= 10; $g++){
for($i = 0; $i <= 4; $i++){
$counter = $g - 7;
if($i != $counter){
continue;
} else {
$data[$grade.[$g]] = $grades[$i]);
}
}
}

PHP for loop with exception rules

I am searching for a php for loop, which should output
22,23,24, 32,33,34, and so on 92,93,94 and all following like 102,103,104,122,123 until 902,903,904,992,993,9949, but not or 12,13,14... 112,13,114 ...912,913,914.
my code:
for ($a = 22; $a <= 1000; $a+=10) {
$b = $a + 1;
$c = $a + 2;
echo "$a <br>";
echo "$b <br>";
echo "$c <br>";
}
I need here an exception, because 112,123 and 124 and so on until 912,913,914 shouldn't be echoed.
All results should be stored in an array.
Try using this code:
$data = array();
for ($a = 2; $a < 10; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
$data[] = $c;
}
}
I have found all parts do finish kwestgrounds answer with my exceptions.
$delete = array_merge(range (112, 912, 100), range (113, 913, 100),range (114, 914, 100));
$data = array();
for ($a = 2; $a < 100; $a++) {
for ($b = 2; $b <= 4; $b++) {
$c = ($a * 10) + $b;
{
if (in_array($c, $delete)) continue;
}
$data[] = $c;
}
}

php merging or concatenation strings when assigning a variable

PHP NOOB, i have been working on this code all day, but cant find any good way to fix it.
here is my code:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
$m.$i = 20;
}
else
{
$m.$i = 10;
}
}
Am trying to join $m.$i together and set it to 20 or 10, but i end up having me20 or me10 instead of me1 = 20 or me1 = 10 when i echo $m.$i which is legit, is there anyways to make this work?
$m.$i = 20;
This will assign $i = 20 and then concatenate it with $m and hence you will see me20.
What you need is $m . $i .= 20; instead. which will concatenate them altogether.
Fixed:
<?php
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
echo $m . $i .= 20;
}
else
{
echo $m.$i .= 10;
}
}
?>
EDIT:
The above answer was a total misunderstanding, I realised you intended to create the variables:
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
echo $me0;
}
else
{
${$m.$i} = 10;
}
}
Assign it like so.
${$m.$i} = 20;
You are trying to dynamically create variables, so you have to do something like this:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
}
else
{
${$m.$i} = 10;
}
}
then try to priny $me0, $me1

Categories