|
-
Apr 10th, 2007, 11:08 AM
#1
Thread Starter
Fanatic Member
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;
}
-
Apr 10th, 2007, 11:15 AM
#2
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.
-
Apr 10th, 2007, 11:21 AM
#3
Thread Starter
Fanatic Member
Re: Efficient use of arrays
 Originally Posted by penagate
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.
-
Apr 10th, 2007, 11:26 AM
#4
Thread Starter
Fanatic Member
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?
-
Apr 10th, 2007, 11:32 AM
#5
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(1, 2, 3, 4, 5, 6, 7, 8);
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.
-
Apr 10th, 2007, 11:47 AM
#6
Thread Starter
Fanatic Member
Re: Efficient use of arrays
 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(1, 2, 3, 4, 5, 6, 7, 8);
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($mybigfatarray, somefunctionIwrotethismorning);
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").
-
Apr 10th, 2007, 12:36 PM
#7
Re: Efficient use of arrays
PHP Code:
function filter($x)
{
return $x['bar'] == 'yomama';
}
$yomamas = array_filter($mybigfatarray, "filter");
?
-
Apr 10th, 2007, 03:19 PM
#8
Thread Starter
Fanatic Member
Re: Efficient use of arrays
 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");
-
Apr 10th, 2007, 03:39 PM
#9
Re: Efficient use of arrays
PHP Code:
$yomamas = array_filter($mybigfatarray, create_function('$x', 'return $x["bar"]=="yomama"'));
$yodadas = array_filter($mybigfatarray, create_function('$x', 'return $x["bar"]=="yodada"'));
$yosista = array_filter($mybigfatarray, create_function('$x', 'return $x["bar"]=="yosista"'));
?
-
Apr 11th, 2007, 09:31 AM
#10
Thread Starter
Fanatic Member
Re: Efficient use of arrays
Oooh that's good. I could make a nice wrapper for that.
-
Apr 12th, 2007, 06:57 AM
#11
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.
-
Apr 12th, 2007, 09:55 AM
#12
Thread Starter
Fanatic Member
Re: Efficient use of arrays
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|