SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Array Help

  1. #1
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Help

    Hello Guys

    I have a situation where I have 4 arrays.

    The contents of the 4 arrays are the same, but I need to know
    how many time each element appears in all the 4 arrays together.

    So if I have a watch called rolex, I need to know how may times it appears in
    each of the 4 arrays.

    Any thoughts please ........

    Didi

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not fully understanding what you are asking.
    If all the contents of the arrays are the same, doesn't 'rolex' appear in all of them (4 arrays, meaning 4 times?)
    Also, I am not quite understanding why you would have 4 arrays with the same contents.

  3. #3
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI Ruben

    Actually , I have a series of checkboxes...when sent for processing,each element of the check box reads watch types from a databse,

    So I could have swiss and a checkbox value and it would read from
    the databse all swiss watch makes and that could be many watches

    So I have 4 forms,each with a series of checkboxes, each value having many values in a database table

    This will mean that I will have from the first form an array and the same for the rest for each value of a particular checkbox,

    Now the element type in each array is the same..ie it could be rolex or it could be swatch or it could be tempo etc.....

    I need to know how many times tempo or rolex appears in each of the arrays

    How this explains my needs

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI

    I think what I need is best explained in this example

    array1={"red","blue","green","purple","orange","yellow","red","green"}
    array2={"red","green","orange","yellow","green","red","green"}
    array3={"red","blue","green","purple","green","orange","yellow","black","white","green"}
    array4={"purple","orange","yellow","black","white","green","white","green"}

    I need a method which will allow me to find out how many time green appears
    in each of the arrays..and also a total of all 4 arrays...

  5. #5
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My suggestion is to have a multidimensional array.

    like
    Code:
    array
    (
       [0] => array( red, blue, green, purple, orange, yellow, red green ),
       [1] => array( red, blue, purple, orange, green ),
       [3] => array( black, purple, green, yellow, red, white, orange ),
    ),
    This way you can loop through the array using foreach.

    PHP Code:
    $green 0;
    foreach( 
    $array as $k => $v )
    {
        foreach( 
    $v as $colors )
        {
            if( 
    $colors == 'green' )
            {
                
    $green++;
            }            
        }

    This should do the trick, then.

  6. #6
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    will this loop thru each of the elements or only a[0] etc

  7. #7
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dd1313
    will this loop thru each of the elements or only a[0] etc
    The first foreach() will loop through [0] until [3].
    The second loops through the colors.

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

    I was also looking for somehing similiar

    works 100%

    N


Bookmarks

Posting Permissions

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