开发者

Multidimensional array filtering

开发者 https://www.devze.com 2023-02-14 08:16 出处:网络
I am trying to put some images to an other image. The short code below is a sample about my trying. but the $i and $j variable is invisible.

I am trying to put some images to an other image. The short code below is a sample about my trying. but the $i and $j variable is invisible.

$positions = array(
         array('64','64','home.png','www.sdsd.vf'),
         array('128','640','icon-building64.png','www.sdsd232.vf')
        );
for($i=0; $i<700; $i+=64)
    for($j=0; $j<1100; $j+=64)
    {
                  $out = array_filter($positions, function($position) {
                  return ($position[0] == $j AND $position[1] == $i);
    }); 
    $out = array_merge(array(),$out);

I tried 开发者_StackOverflow社区this but I get errors:

    $out = array_filter($positions, function($position,$i,$j) {
                  return ($position[0] == $j AND $position[1] == $i);
    }); 

Thanks for your helps.


You cant pass extra arguments to the array_filter function, any filtering should occur in there, not in your loops like so:

function filterPositions($value) {
  return
    ($value[0] < 1100 && $value[1] < 700) &&
    ($value[0] % 64 == 0) && ($value[1] % 64 == 0);
}
$out = array_filter($positions, 'filterPositions');


The best way to do this, is passing the $i and $j to your anonymous function

$out = array_filter($positions, function($position) use ($i, $j) {
                  return ($position[0] == $j AND $position[1] == $i);
    });

This way you will be avoiding hardcoding values in function.

0

精彩评论

暂无评论...
验证码 换一张
取 消