[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

PHP Two Dimensional Index Array

I'm used to creating two dimensional array something like this:

$theNewMatrix = array();
	for ($i = 0; $i == 5; $i++)
	{
		for ($j = 0; $j == 4; $j++)
		{$theNewMatrix[$i][$j] = $j;}
	}

Open in new window


In PHP what's the best way to fill a 'two dimensional' (I realize php does arrays of arrays) similar to how I would expect the above to act.

Thanks!
Christa
0
christamcc
Asked:
christamcc
  • 6
  • 3
  • 3
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
That should work fine.  What are you looking for?
0
 
christamccAuthor Commented:
hmm, when i do a :

echo count($theNewMatrix);

Open in new window


after I run the initialization of the array I get "0"

and when I print it into an html table it's empty
0
 
NerdsOfTechTechnology ScientistCommented:
use COUNT_RECURSIVE parameter
echo count($theNewMatrix, COUNT_RECURSIVE);

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
christamccAuthor Commented:
I tested both count and my print function on the result of this code below, which is what I expect the loop above to give me, and then both output what I expect.  I need a loop, like in my original question, that will initiate the matrix to give me the same result as this.

$theMatrix[0][0] = 0;
$theMatrix[0][1] = 1;
$theMatrix[0][2] = 2;
$theMatrix[1][0] = 0;
$theMatrix[1][1] = 1;
$theMatrix[1][2] = 2;
$theMatrix[2][0] = 0;
$theMatrix[2][1] = 1;
$theMatrix[2][2] = 2;

Open in new window



0
 
NerdsOfTechTechnology ScientistCommented:
oh i see try this. this should work
$theNewMatrix = array();
$tempJ = array();

for ($i = 0; $i == 5; $i++)
{
 for ($j = 0; $j == 4; $j++)
 {
  $tempJ[] = $j; // add value of j to single-dim temp array. j implied index
 }
 $theNewMatrix[] = $tempJ; // add temp array in multi-dim array. i implied as index.
 $tempJ = array(); // clear temp array
}

echo count($theNewMatrix);
echo count($theNewMatrix, COUNT_RECURSIVE);

Open in new window

0
 
NerdsOfTechTechnology ScientistCommented:
fixed & tested

temp array is recommenced (see code)
$theNewMatrix = array();
$tempJ = array();

for ($i = 0; $i <= 5; $i++)
{
 for ($j = 0; $j <= 4; $j++)
 {
  $tempJ[] = $j; // add value of j to single-dim temp array. j implied index
  echo $i . ", " . $j . "<br>";
 }
  $theNewMatrix[] = $tempJ; // add temp array in multi-dim array. i implied as index.
  $tempJ = array(); // clear temp array
}

// *** DEBUG ***

 print_r ($theNewMatrix); // print array info in human readable
 echo "<br>" . count($theNewMatrix) . "<br>"; // print 1st dim count
// expected output: 6 containers (i) = 6

 echo count($theNewMatrix, COUNT_RECURSIVE); // print all dims and all items count
// expected output: 5 items (j) in 6 containers (i) = i*j + i = 5*6 + 6 = 36

Open in new window

0
 
NerdsOfTechTechnology ScientistCommented:
line 9 is debug and can be commented out.
0
 
NerdsOfTechTechnology ScientistCommented:
for fun we will also output the COUNT of just the ITEMS only on the last line. expected output of 30....
containers 0,1,2,3,4,5 (6 containers) with 0,1,2,3,4 (5 items)

in for loop use <= instead of ==
$theNewMatrix = array();
$tempJ = array();

for ($i = 0; $i <= 5; $i++)
{
 for ($j = 0; $j <= 4; $j++)
 {
  $tempJ[] = $j; // add value of j to single-dim temp array. j implied index
  echo $i . ", " . $j . "<br>";
 }
  $theNewMatrix[] = $tempJ; // add temp array in multi-dim array. i implied as index.
  $tempJ = array(); // clear temp array
}

// *** DEBUG ***

print_r ($theNewMatrix); // print array info in human readable
echo "<br>" . count($theNewMatrix) . "<br>"; // print 1st dim count
// expected output: 6 containers (i) = 6

echo count($theNewMatrix, COUNT_RECURSIVE) . "<br>"; // print all dims and all items count
// expected output: 5 items (j) in 6 containers (i) plus 6 containers = i*j + i = 5*6 + 6 = 36

echo (count($theNewMatrix, COUNT_RECURSIVE) - count($theNewMatrix));
// expected output: items ONLY. 5 items (j) in 6 containers (i) = 5*6 = 30

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
I used your code to make a page like @NerdsOfTech above and it worked when I changed the '==' to '<'.  I used the same loop to display the values as to assign them.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP count array</title>
</head>
<body>
<h1>PHP count array</h1>
<?php 
$theNewMatrix = array();
for ($i = 0; $i < 5; $i++) {
	for ($j = 0; $j < 4; $j++) {
		$theNewMatrix[$i][$j] = $j;
		}
	}

echo count($theNewMatrix, COUNT_RECURSIVE);
echo "<br>";

for ($i = 0; $i < 5; $i++) {
	echo "<br>";
	for ($j = 0; $j < 4; $j++) {
		echo $theNewMatrix[$i][$j]." ";
		}
	}
echo "<br>";	
?>
 
</body>
</html>

Open in new window

0
 
christamccAuthor Commented:
Thanks all!  I just figured out the same thing... "==" = bad....   I was so focused on my unfamiliarity with PHP arrays that I forgot to check the simple loop!

I'm going to use my same code as corrected by Dave.
0
 
Dave BaldwinFixer of ProblemsCommented:
Thanks.
0
 
NerdsOfTechTechnology ScientistCommented:
Thanks for the direct multi-dim item assignment syntax refresh. I don't know what I was thinking when I coded that mess of an answer earlier. LOL :) Thanks to you and thanks to chistamcc for the assist points!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now