Jump to content

[SOLVED] Count and multidimensional arrays


SchweppesAle

Recommended Posts

hi, I have the following code

 

for($i = 0; $i < count($update); $i++)			
		{



		}	

 

$update[$i][$y] is a two dimensional array, the count function will return the number of values $i however I'd like to add an additional statement which will pull the "highest pointer" [$y] so that I can test each of the [$y] pointers for dimension [$i].

 

I hope that makes sense.  Basically, I'm storing an id value within the pointer itself.  I need to run  a check on each element of the array then update the database should the value associated with that pointer(which is also an id within the database) is different from what's already in there.

Link to comment
Share on other sites

for($i = 0; $i < count($update); $i++)         
{
    for($j=0; $j < count($update[$i]); $j++
    {
          //check here using $update[$i][$j]

    }             
            
         
}   

Link to comment
Share on other sites

We'll need some more information or some sample data, maybe this helps?

 

$highestPointer = 0;
$sizeOf = sizeof($update);
for ($i = 0; $i < $sizeOf; ++$i) {
    $pointer = max($update[$i]);
    if ($pointer > $highestPointer) {
        $highestPointer = $pointer;
    }
}

Link to comment
Share on other sites

I would advise not using count($array) within a for() loop for two reasons:

 

1. The foreach operator was designed for arrays

2. The for loop has to continually calculate the size of the array on each iterration. When using a for loop it is more efficient to define a value before the loop instead of recacluating each time (there are exceptions to this).

 

Anyway, this is how I would handle this:

 

foreach($update as $subarray)
{
    foreach ($subarray as $value)
    {
        //$value = $update[$i][$j] from your initial example
    }
}

Link to comment
Share on other sites

To supplement mjdamato; if you also want to have access to the keys, use as key => value:

 

foreach($update as $i => $subarray)
{
    foreach ($subarray as $j => $value)
    {
        //$value = $update[$i][$j] from your initial example
    }
}

Link to comment
Share on other sites

ok, here's an example

 

array[1][24] = micro;

array[2][40] = soft;

array[3][35] = blows;

 

The id for each of these items would be stored within the second bracket.  so there's an entry within the database with an id of 24 which may/may not be micro.  The problem isn't so much retrieving the element with the highest value, but rather the highest pointer. 

 

if($update != NULL)	
{		



		for($i = 0; $i < count($update); $i++)			
		{
			for($j = 0; $j < max($update[$i]); $j++)
			{
				if($update[$i][$j] != NULL)
				{
					$updating = 'works';
				}
			}



		}	

}

 

Unfortunately, the above code will pull the highest value $y for that array. 

Link to comment
Share on other sites

To supplement mjdamato; if you also want to have access to the keys, use as key => value:

 

foreach($update as $i => $subarray)
{
    foreach ($subarray as $j => $value)
    {
        //$value = $update[$i][$j] from your initial example
    }
}

 

lol, wait that's a good idea.  I'll try that then

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.