...

View Full Version : Arrays.



apoth
04-20-2004, 09:42 AM
I've got an array of arrays - $index.
I check and see if the 'id' field (element 8) in each element array of the $index array matches the id field in a $temp array.
If so it's supposed to increment the 'frequency' field (element 0) in the array.

As things stand though, this code doesn't increment the frequency field in the array that's in the $index array - just for $var it seems.

Presumably the solution is to remove the selected $var from the $index, increment the frequency field and add it back to the array. Can anyone help with how I'd do this? I hope it's all clear!


foreach ($index as $var) {
if ($var[8] == $temp[8]) {// if the id matches they're the same
/* increment frequency THIS DOESN'T WORK */
$var[0] = $var[0]+1;
}
}

Many thanks

raf
04-20-2004, 10:31 AM
Not sure i follow this
$index has arrays as elements, right? So you can't increment an element of indew as a counter. Your current code will increment the first element of the $var arrays (the first element of the elements of $index.
If you need that counter on the top level, then you'll need to have an array like


$temp= array('1','2','3','4','5','6','7','8','9');
$index= array('0',array('1','3','4','5','6','7','8','9'),
array('1','4','4','5','6','7','8','9'),
array('2','10','4','5','6','7','8','9'));
$counter = $index[0];
foreach($index as $var){
if ($var[8]==$temp[8]){
$var[0]++;
$counter ++;
}
}
$index[0]=$counter;

sidney
04-20-2004, 10:55 AM
foreach ($index as $key =>$var) {
if ($var[8] == $temp[8]) {// if the id matches they're the same
$index[$key][0]++;
}
}

apoth
04-20-2004, 11:12 AM
Cheers guys, I need the frequency counter for each array within the array (yes, the elements of the $index array are arrays themselves). So I went with Sidney's suggestion - spot on :) Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum