|
-
Apr 10th, 2007, 11:47 AM
#1
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").
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
|