View Full Version : Arrays.

04-20-2004, 10: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

04-20-2004, 11: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'),
$counter = $index[0];
foreach($index as $var){
if ($var[8]==$temp[8]){
$counter ++;

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

04-20-2004, 12:12 PM
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.