SchweppesAle Posted July 8, 2009 Share Posted July 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/ Share on other sites More sharing options...
Bendude14 Posted July 8, 2009 Share Posted July 8, 2009 for($i = 0; $i < count($update); $i++) { for($j=0; $j < count($update[$i]); $j++ { //check here using $update[$i][$j] } } Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/#findComment-871125 Share on other sites More sharing options...
ignace Posted July 8, 2009 Share Posted July 8, 2009 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/#findComment-871129 Share on other sites More sharing options...
Psycho Posted July 8, 2009 Share Posted July 8, 2009 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/#findComment-871140 Share on other sites More sharing options...
thebadbad Posted July 8, 2009 Share Posted July 8, 2009 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/#findComment-871145 Share on other sites More sharing options...
SchweppesAle Posted July 8, 2009 Author Share Posted July 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/#findComment-871149 Share on other sites More sharing options...
SchweppesAle Posted July 8, 2009 Author Share Posted July 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/#findComment-871152 Share on other sites More sharing options...
SchweppesAle Posted July 8, 2009 Author Share Posted July 8, 2009 you're the man, I didn't know you could access the keys like that with a foreach. Thanks though, this was just what i needed. Quote Link to comment https://forums.phpfreaks.com/topic/165203-solved-count-and-multidimensional-arrays/#findComment-871156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.