Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Arrays.

  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrays.

    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!

    Code:
    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

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    $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
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    foreach ($index as $key =>$var)    {
        if (
    $var[8] == $temp[8])    {// if the id matches they're the same
            
    $index[$key][0]++;
        }


  • #4
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •