Jump to content

Array Dynamic Number Table


shamuraq

Recommended Posts

Hi all,

I am trying to create a dynamic number table but keep getting an error during parsing

<?
$fraction02 = array();
$multiplier = 10;
for($i = 0; $i < $multiplier; $i++){
$fraction02[$i] = $i + 1;
	for($j = 0; $j < $multiplier; $j++){
		$fraction02[$i][$j] = ($j + 1) * $i;
	}
echo $fraction02[$i].'<br>';
}
?>

The error that i got is:

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
1
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
2
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
3
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
4
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
5
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
6
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
7
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
8
Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
9

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23

Warning: Cannot use a scalar value as an array in D:\Apache Group\...frac_gen01.php on line 23
10

Thanx in advance... I've got a feeling this is just minor tweaking but my "blurred" brain just refuse to point at it...

Link to comment
Share on other sites

Okay.1 question

what is

$fraction02[$i] = $i + 1;

used for ?

 

as that sets $fraction02[$i] to a number BUT you want to use it as an array!

 

So i assume you wanted this

<?php
$fraction02 = array();
$multiplier = 10;
for($i = 0; $i < $multiplier; $i++){
//$fraction02[$i] = $i + 1;
for($j = 0; $j < $multiplier; $j++){
	$fraction02[$i][$j] = ($j + 1) * $i;
}
//echo ($i+1).'<br>';
}

//output - whatever
foreach($fraction02 as $K1 => $V1){
echo "<H1>$K1</H1>\n";
foreach($V1 as $K2 => $V2){
	echo "$K2 => $V2<br>\n";
}
}
?>

Link to comment
Share on other sites

i didn't wait for the reply and by chance tuned the code close to urs (MadTechie).

<?
$fraction02 = array();
$multiplier = 20;
for($i = 1; $i < $multiplier; $i++){
	for($j = 0; $j < $multiplier; $j++){
		$fraction02[$i] = array();
		$fraction02[$i][$j] = ($j + 1) * $i;
		echo $fraction02[$i][$j].", ";//Just to test for result
	}
echo '<br>'; //Just to test for result
}
?>

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60,
4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80,
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102, 108, 114, 120,
7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140,
8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160,
9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 135, 144, 153, 162, 171, 180,
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132, 143, 154, 165, 176, 187, 198, 209, 220,
12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180, 192, 204, 216, 228, 240,
13, 26, 39, 52, 65, 78, 91, 104, 117, 130, 143, 156, 169, 182, 195, 208, 221, 234, 247, 260,
14, 28, 42, 56, 70, 84, 98, 112, 126, 140, 154, 168, 182, 196, 210, 224, 238, 252, 266, 280,
15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 272, 288, 304, 320,
17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 272, 289, 306, 323, 340,
18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198, 216, 234, 252, 270, 288, 306, 324, 342, 360,
19, 38, 57, 76, 95, 114, 133, 152, 171, 190, 209, 228, 247, 266, 285, 304, 323, 342, 361, 380, 

But this is where the problem starts. When i add a print_r statement:

print_r($fraction02);

It only shows array [19] of each array:

Array ( [1] => Array ( [19] => 20 ) [2] => Array ( [19] => 40 ) [3] => Array ( [19] => 60 ) [4] => Array ( [19] => 80 ) [5] => Array ( [19] => 100 ) [6] => Array ( [19] => 120 ) [7] => Array ( [19] => 140 ) [8] => Array ( [19] => 160 ) [9] => Array ( [19] => 180 ) [10] => Array ( [19] => 200 ) [11] => Array ( [19] => 220 ) [12] => Array ( [19] => 240 ) [13] => Array ( [19] => 260 ) [14] => Array ( [19] => 280 ) [15] => Array ( [19] => 300 ) [16] => Array ( [19] => 320 ) [17] => Array ( [19] => 340 ) [18] => Array ( [19] => 360 ) [19] => Array ( [19] => 380 ) )

What happened to the rest of the values that was initially present during the echoed loop?

Link to comment
Share on other sites

That's be cause your clearing it out

 

ie look at this and follow the logic

$fraction02 = array();
$fraction02[0] = array();
$fraction02[0][1] = "hello";
$fraction02[0] = array();
$fraction02[0][2] = "world";
print_r($fraction02);

 

So in your code

$fraction02[$i] = array();

should be in the previous loop (move it up 2 lines) ;)

 

Ahhh @ wildteen88, I knew I shouldn't of typed an example!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.