Help with multidimensional array

Hi all,

First post here and a php newbie so please be gentle!

I’m trying to learn php and have a script I’m trying to breakdown to understand some of the functionality (don’t worry the script is freely available and I’m just using it to learn!).

The script is taking an image and plotting certain points in the image. I am starting to get to grips with a lot of the script now, but there is a tricky bit I just can’t get my head around.

I’m just wondering if a kind soul here would be able to explain to me what the below is doing exactly? I have started to understand arrays - mainly from the php manual (but I think this is getting into multidimensional arrays which is a bit beyond me right now - I’m not even sure this is one!)

The script defines $pattern = array() then starts populating the array with co-ordinates (x and y) from an image that is loaded.

[php]for ($x = 0; $x < $numPages; $x++) //loop through width/columns
{
$colorAbove = “None”; //reset for beginning of a column
$changes = FALSE;
$numColorBands = -1;
for ($y = 0; $y < $imageheight; $y++)//loop through height
{
$currentColor = imagecolorat($image ,$x ,$y);

		if ($colorAbove === "None" and $currentColor == 0)//if picture is black at the top border create start of a color band
		{
			$colorAbove = $currentColor;
			$numColorBands += 1;
			$pattern[$x][$numColorBands] = array("start"=>0);
			$changes = TRUE;
		}
		elseif ($currentColor == 0 and $y == $imageheight-1) //create end mark if the bottom pixel is black
		{
			$pattern[$x][$numColorBands]["end"] = $y+1;
			$changes = TRUE;
		}
		elseif ($colorAbove === "None" or $colorAbove == $currentColor)//do nothing at begin of column if the first pixel is white, or if color doesn't change
		{
			$colorAbove = $currentColor;
			continue;
		}
		elseif ($colorAbove == 1 and $currentColor == 0) //create mark if color goes from white to black
		{
			$colorAbove = $currentColor;
			$numColorBands += 1;
			$pattern[$x][$numColorBands] = array("start"=>$y);
			$changes = TRUE;
		}
		elseif ($colorAbove == 0 and $currentColor == 1) //create mark if color goes from black to white
		{
			$colorAbove = $currentColor;
			$pattern[$x][$numColorBands]["end"] = $y+1;
			$changes = TRUE;
		}
	}
	if ($changes == FALSE)
		{
			$pattern[$x] = "nofolds"; //mark no folds, these will only be allowed at start or end of pattern
		}
}
return ($pattern);[/php]

I’m understanding what we are doing with a loop here and where and why it is marking points based on pixel colours in the image, what I don’t understand is how it is storing these in the $pattern array.

Why do we have double, sometimes triple, [] when writing to the array? What does this do?

[php]$pattern[$x][$numColorBands] = array(“start”=>0);[/php]

[php]$pattern[$x][$numColorBands][“end”] = $y+1;[/php]

Please be assured that I have literally spent hours and hours trying to figure this out for myself but it’s becoming frustrating now that I don’t understand it!!! :o

Any help or advice would be truly appreciated!

Many thanks in advance
EB

the pattern is like this

[php]$variableName[‘level1’][‘level2’][‘level3’][‘etc’][/php]

So the following line

[php]$pattern[$x][$numColorBands] = array(“start”=>0);[/php]

Will insert array(“start” => 0) as the value for the key $numColorBands, which is under the key $x, which is in the array $pattern.

If we assume $x is 1 and $numColorBands is 10, then the above line will look like this

[php]Array(
1 => Array(
10 => Array(
“start” => 0
)
)
)[/php]

You can pretty print php variables like this:

[php]$array = array(‘some’ => array(‘foo’ => 1, ‘bar’ => 2));

echo ‘

’;
print_r($array);[/php]

Which will give you the output

[php]Array
(
[some] => Array
(
[foo] => 1
[bar] => 2
)

)[/php]

Here you can clearly see that to get the “2” value you would do $array[‘some’][‘bar’]

Hi Jim,

Many thanks for your kind reply.

I can see the loop is going through each iteration of $x which is the x axis on the image and within that loop, looping through each y axis point. We start at x=0. So if I went through 2 iterations of x for this example, I guess the array would look something like this…?

[php]
Array ( //the $pattern array
0 => Array ( //the $x array
1 => Array ( //the $numColorBand array
“start” => 25
“end” => 50
)
2 => Array ( //the $numColorBand array
“start” => 75
“end” => 92
)
3 => Array ( //the $numColorBand array
“start” => 100
“end” => 150
)
)
1 => Array ( //the $x array
1 => Array ( //the $numColorBand array
“start” => 30
“end” => 55
)
2 => Array ( //the $numColorBand array
“start” => 80
“end” => 97
)
3 => Array ( //the $numColorBand array
“start” => 105
“end” => 155
)
)
)
[/php]

Does that sound about right? If it does, that is making much more sense!!!

A very dumb question also, in php if on an image we are at $x=0 and $y=o are we in the top left corner or the bottom left corner?

Thanks once again!

There are no such thing dumb/stupid questions, only stupid answers :stuck_out_tongue:

Don’t guess what the resulting array looks like, check it (I wrote how) :slight_smile:

:stuck_out_tongue: Many thanks!

Seems to be working as expected!

[php]Array
(
[$x] => Array
(
[0] => Array
(
[cb1] => Array
(
[start] => 25
[end] => 50
)

            )

    )

)[/php]

Another query if I may!

I cannot find what out what the % does int he following line, are you able to help me with this?

[php]$finalPattern[$column] = array($bandslist[$column%$numBands]);[/php]

Thanks again

Google: what does the percent sign mean in php

What exactly does this mean?

[php]$number = ( 3 - 2 + 7 ) % 7;[/php]

It's the modulus operator, as mentioned, which returns the remainder of a division operation.

Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.

5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.

10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.

In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.

Now why couldn’t I find that!! ???

Than you once again Jim, you have been an amazing help. Just a little explanation from you and I have a much better understanding of something I have been staring at for hours upon end!

Very much appreciated

EB

Hi again,

Just hoping someone can give me a little more help on this.

So, far I have an array $pattern. Looks something like this

[php]Array
(
[$x] => Array
(
[0] => Array
(
[cb1] => Array
(
[start] => 25
[end] => 50
)

                [cb2] => Array
                    (
                        [start] => 26
                        [end] => 51
                    )

            )

        [1] => Array
            (
                [cb1] => Array
                    (
                        [start] => 25
                        [end] => 50
                    )

            )

    )

)[/php]

Based on this $pattern array there is another function

[php]function createFinalPattern($pattern)
{
$finalPattern = array();
foreach ($pattern as $column => $bandslist)
{
$numBands = count($bandslist);
if ($numBands == 1)
{
$finalPattern[$column] = $bandslist;
continue;
}
elseif ($numBands > 6)
{
echo “\nToo Many.\n”;
exit;
}
else
{
$finalPattern[$column] = array($bandslist[$column%$numBands]);
}
}
return ($finalPattern);
}[/php]

I can see that in the $pattern array if the key only has a count of 1 then it loads into the $finalpattern array the key and the values

I can’t work out what it is doing if there is more than 1 (but less than 6)
I now know that the $column%$numBands is getting the remainder of $column / $numBands but what is it doing with the remainder here? How would the array be structured?

[php]$finalPattern[$column] = array($bandslist[$column%$numBands]);[/php]

Many thanks again
EB

Hi

Unfortunately I still can’t work out the final line in the previous post.
Would anyone be able to explain what’s happening please?

Thanks once again
EB

Apologies for bumping this again but is anyone able to help me understand this final line above?

Many thanks for your help

EB

Sponsor our Newsletter | Privacy Policy | Terms of Service