Results 1 to 12 of 12

Thread: Efficient use of arrays

  1. #1

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Efficient use of arrays

    Now the fastest way to do this would be with an SQL SELECT statment but as this is not possible I need to have reasonable speed even with big arrays

    Belo are my functions which are logically simple. However is there a way of getting the same results but less processor cycles?

    PHP Code:
        // returns array
        
    function all_x_where_y_equals($x$y$val$from){
            
    $new = array();
            foreach (
    $from as $fr){
                if (
    $fr[$y] == $val){
                    
    $new[] = $fr[$x]
                }
            }
            return 
    $new;
        }
        
        
    // returns array
        
    function all_keys_where_y_equals($y$val$from){
            
    $new = array();
            foreach (
    $from as $k => $fr){
                if (
    $fr[$y] == $val){
                    
    $new[] = $k
                
    }
            }
            return 
    $new;
        } 
    ?
    'What's this bit for anyway?
    For Jono

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Efficient use of arrays

    There are a huge number of built-in array functions:
    http://au2.php.net/manual/en/ref.array.php

    I daresay you could find two to do what you've done there, and most likely faster as well.

  3. #3

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Efficient use of arrays

    Quote Originally Posted by penagate
    There are a huge number of built-in array functions:
    http://au2.php.net/manual/en/ref.array.php

    I daresay you could find two to do what you've done there, and most likely faster as well.
    I figure that an inherently itterative function (or evena call abck one would probably work. I know that there is a filter with call abck (no idea how to use it) but I wouldn't know what function to plumb for if it came and hit me with a large textbook.

    Sandly while I have very much grasped the logic the fine practice of code itself sometimes gets the better of me.
    ?
    'What's this bit for anyway?
    For Jono

  4. #4

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Efficient use of arrays

    The other issue is are array_walk and array_filter and faster than itteratin (which we do anyway)?

    There seems to be a lack of flexability (that or I'm not getting the full scope of passing an array as a callback argument...

    http://au2.php.net/manual/en/languag...types.callback

    EDIT: I think my problem is tat I want to define paramiters in my itteration at call time containing some values supplied at runtime. I know that basic set opperatons exist for arrays. I don't know much about them would they be of use though?
    ?
    'What's this bit for anyway?
    For Jono

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Efficient use of arrays

    You don't pass the array to the callback parameter. The callback is a reference to a function that you write to implement your comparison logic. In this case the $x/$y business in your code (which I have not quite figured out - me a bit slow this morning).

    For example:
    PHP Code:
    $numbers = array(12345678);

    function 
    odd($x)
    {
      return 
    $x 1;
    }

    $odd_numbers array_filter($numbers"odd"); 
    Bah... I just noticed they used this example on the doc page as well. But that's the gist of it.

  6. #6

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Efficient use of arrays

    Quote Originally Posted by penagate
    You don't pass the array to the callback parameter. The callback is a reference to a function that you write to implement your comparison logic. In this case the $x/$y business in your code (which I have not quite figured out - me a bit slow this morning).

    For example:
    PHP Code:
    $numbers = array(12345678);

    function 
    odd($x)
    {
      return 
    $x 1;
    }

    $odd_numbers array_filter($numbers"odd"); 
    Bah... I just noticed they used this example on the doc page as well. But that's the gist of it.
    I followed that well enough, perhaps if I cover what I'm up to in the function I will crack it or enlighten you as to how odd I am (watever happens soonest).

    Example data

    Code:
    Array(
      0 => array('foo' => 1, 'bar' => 45),
      1 => array('foo' => 77, 'bar' => 'yomama'),
      2 => array('foo' => 'cats', 'bar' => 'ags')
    )
    Thing is the real array migh be huge it is identical to piles of rows from a atabase (because in any other system it would be)

    I want to be able to do the code only equivilant of "SELECT * FROM TABLE WHERE FIELD=VALUE"

    so the first funcion returns all foo where bar=var and the second gets the keys (row numbers for all rows where bar=var).

    The quickest is just to inspect them all

    Now with array_filter (my best bet I think) I can go

    PHP Code:
    $bob array_filter($mybigfatarraysomefunctionIwrotethismorning); 
    but how do I tell the function what the bar=var is that we are filtering for?

    That's where I came unstuck. (it been a long day for me).

    The other question is: is running a callback to itterate an array faster than itterating the array? If not what is. My worry is that the array could geta bit sizable (it's got to grow somewhat with use being part of a script designed to turn news into silliness via mapping words in a complex but interesting way untill the results are "funny").
    ?
    'What's this bit for anyway?
    For Jono

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Efficient use of arrays

    PHP Code:
    function filter($x)
    {
      return 
    $x['bar'] == 'yomama';
    }

    $yomamas array_filter($mybigfatarray"filter"); 
    ?

  8. #8

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Efficient use of arrays

    Quote Originally Posted by penagate
    PHP Code:
    function filter($x)
    {
      return 
    $x['bar'] == 'yomama';
    }

    $yomamas array_filter($mybigfatarray"filter"); 
    ?
    The problem is...

    PHP Code:
    $yomamas array_filter($mybigfatarray"filter");
    $yodadas array_filter($mybigfatarray"filter");
    $yosista array_filter($mybigfatarray"filter"); 
    ?
    'What's this bit for anyway?
    For Jono

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Efficient use of arrays

    PHP Code:
    $yomamas array_filter($mybigfatarraycreate_function('$x''return $x["bar"]=="yomama"'));
    $yodadas array_filter($mybigfatarraycreate_function('$x''return $x["bar"]=="yodada"'));
    $yosista array_filter($mybigfatarraycreate_function('$x''return $x["bar"]=="yosista"')); 
    ?

  10. #10

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Efficient use of arrays

    Oooh that's good. I could make a nice wrapper for that.
    ?
    'What's this bit for anyway?
    For Jono

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Efficient use of arrays

    Here's a version that doesn't require the ugly create_function. (PHP4 object syntax)
    Code:
    class elemequals_functor
    {
      var $val;
      var $elem;
      function elemequals_functor($e, $v) { $this->elem = $e; $this->val = $v; }
      function run($a) { return $a[$e] == $val; }
    }
    function elemequals($e, $v)
    {
      return array(new elemequals_functor($e, $v), 'run');
    }
    
    // For your particular case.
    function bareq($v)
    {
      return elemequals('bar', $v);
    }
    
    $yomamas = array_filter($mybigfatarray, bareq("yomama"));
    $yodadas = array_filter($mybigfatarray, bareq("yodada"));
    $yosistas = array_filter($mybigfatarray, bareq("yosista"));
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Efficient use of arrays

    Quote Originally Posted by CornedBee
    Here's a version that doesn't require the ugly create_function. (PHP4 object syntax)
    Very clever. I think I follow all that.

    Thank you.
    ?
    'What's this bit for anyway?
    For Jono

Posting Permissions

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



Click Here to Expand Forum to Full Width