Announcement

Collapse
No announcement yet.

Understanding the miva_array_filter()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Understanding the miva_array_filter()

    I'm trying to understand the miva_array_filter() function. Ref: http://www.mivascript.com/item/miva_...ilter_ref.html

    Syntax: miva_array_filter_ref( array var, offset, element var, filter_expression, output var )

    Return value: Count of output array elements.
    -- array var = Source array of structures.
    -- offset = index in the array to start filtering from.
    -- element var = Name of a variable to reference in the filter expression.
    -- filter_expression = Text of an expression to execute based on the individual array element.
    -- output var = Output array of all structures that tested true in the filter expression.

    Given this example:
    Code:
    <MvASSIGN NAME = "l.method_count"
        VALUE = "{ miva_array_filter( l.methods, 1, l.method, 'l.method:paymentcardtype:id GT 0', l.methods ) }">
    Where: The element l.method is a reference to the structure member l.methods[n]:method

    I'm unclear what the filter_expression is doing.

    So if expressed as a mvt:foreach loop what does the mvt:if statement look like?
    Code:
    <mvt:foreach iterator="item" array="l.methods">
        <mvt:if expr="expression">
    
        </mvt:if>
    </mvt:foreach>
    Also in this example the output is the original array so I assume its being overwritten with the filtered results. Is that true?
    Ray Yates
    "If I have seen further, it is by standing on the shoulders of giants."
    --- Sir Isaac Newton

    #2
    Hi Ray,

    It is true that the output array does overwrite the original array.

    Do you have an array method:paymentcardtype present or assigned to the page ? If so, my best guess, the expression will filter out any paymentcardtype:id with 0 or -1 results (typically an error response code).

    I haven't seen paymentcardtype array used on my OPAY page.

    http://www.alphabetsigns.com/

    Comment


      #3
      Sorry I didn't word the post better.

      What does the internal loop and IF expression look like (given the example above)?

      I'm trying to make sure I understand the parameters element and filter_expression and will add a clarification to Mivascript.com

      This is my best guess/approximation as to what its doing. Is it correct?
      Code:
      <MvFOREACH ITERATOR = "l.method" ARRAY = "l.methods">
          <MvIF EXPR="{ l.method:paymentcardtype:id GT 0 }">
              <MvASSIGN NAME="l.output_count" VALUE="{ miva_array_insert_ref( l.outout, l.method, -1 ) }">
          </MvIF>
      </MvFOREACH>
      Ray Yates
      "If I have seen further, it is by standing on the shoulders of giants."
      --- Sir Isaac Newton

      Comment


        #4
        Hi Ray,
        I think your best guess is correct.

        For me, the example's naming convention is a little confusing as l.method is used in two different places.

        Here is an example I use:

        Code:
        <mvt:assign name="g.filter_expression" value="'l.settings:filter_element:sku NE ' $ g.discontinue_prepared" />
        <mvt:assign name="g.filter_fts_search_prod_length" value="miva_array_filter( l.settings:fts_search_prod:results, g.start, l.settings:filter_element, g.filter_expression, l.settings:fts_search_prod:results )" />
        Hopefully the function author will chime in.

        http://www.alphabetsigns.com/

        Comment


          #5
          And thank you for your awesome work maintaining Mivascript.com, I would be lost without it.
          http://www.alphabetsigns.com/

          Comment


            #6
            Originally posted by alphabet View Post
            Hi Ray,
            I think your best guess is correct.

            For me, the example's naming convention is a little confusing as l.method is used in two different places.
            Agreed. Once if figured out that "element" is the iterator variable it made the filter_expression more clear.

            I did a bit of testing on 2500 records and miva_array_insert_ref() is more than twice as fast as miva_array_insert() and will undoubtedly use less memory.
            The the former's output creates pointers to the original data (so editing one edits the other) while the latter creates a copy of the data.

            Either way, the function is a very fast way to see if a value is contained within a data set.
            Code:
            <mvt:assign name="g.filter_count" value="miva_array_filter_ref( l.settings:all_products:products, 1, l.product, 'l.product:price GE 100', l.settings:output )" />
            Ray Yates
            "If I have seen further, it is by standing on the shoulders of giants."
            --- Sir Isaac Newton

            Comment


              #7
              When I need more info about a particular function, I sometimes search the LSK for a place where the function is used.
              Kent Multer
              Magic Metal Productions
              http://TheMagicM.com
              * Web developer/designer
              * E-commerce and Miva
              * Author, The Official Miva Web Scripting Book -- available on-line:
              http://www.amazon.com/exec/obidos/IS...icmetalproducA

              Comment


                #8
                I added a more detailed explanation of the function on MivaScript.com

                My testing shows miva_array_filter_ref() is approximately twice as fast as miva_array_filter(), when filtering test data of about 2500 records. In either case, the functions provide a very fast way (as in milliseconds) to loop through any data set. I found it used within Merchant for facet filtering and in other places in the LSK.
                Ray Yates
                "If I have seen further, it is by standing on the shoulders of giants."
                --- Sir Isaac Newton

                Comment

                Working...
                X