0

I have a simple function:

function test(){

  //some code

  $X_has_val = $Y_has_val= array();

  foreach ($A as $id => $row){
        if(is_take($id)){
            $X_has_val[$id] = $row;
        }
    }

   foreach ($B as $id => $row){
        if(is_take($id)){
            $Y_has_val[$id] = $row;
        }
    }
  //some code 
}

I did this for get the equivalence

function test(){
  //some code
   $X_has_val = $Y_has_val= array();
  foreach(array($A, $B) as $key=>$value){
       foreach ($value as $id => $row){
            if(is_take($id)){
                $X_has_val[$id] = $row;
                continue;
                $Y_has_val[$id] = $row;
             }
        }
  }
  //some code 
}

1 Answer 1

2

Looks like all you need is array_filter()

$X_has_cc = array_filter($A, 'isTake');
$Y_has_cc = array_filter($B, 'isTake');

like e.g. in

<?php
test();

function test() {
    $A = array(1,2,3,4,5,6,7,8,9,10);
    $B = array(99,100,101,102,103,104);

    $X_has_cc = array_filter($A, 'isTake');
    $Y_has_cc = array_filter($B, 'isTake');

    var_dump($X_has_cc, $Y_has_cc);
}

// select "even elements"
function isTake($x) {
    return 0==$x%2;
}

(and sorry, no, your approach doesn't make much sense ;-))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.