1

Here is the non-functional code. It does not return anything. Not quite sure what is wrong with the syntax I am using.

 function findNeedle($array, $needle) {
        return array_values(array_filter($array, function($arrayValue) use($needle) { return $arrayValue['lp_url'] == $needle; } ));
 }
 $myarray =
         0 =>
         array (
           'lp_url' => 'http://example.com/nx/?utm_source=aa&utm_medium=referral',
           'lp_term_id' => 1435949468,
           'aff_term_id' => 1445295565,
           'offer_term_id' => 1445295996,
             ),
         1 =>
         array (
            'lp_url' => 'http://example.org/nx/?utm_source=aa&utm_medium=referral',
            'lp_term_id' => 1435949468,
            'aff_term_id' => 1445295559,
            'offer_term_id' => 1445295989,
            ),
         );

$needle = 'http://example.com/nx/?utm_source=aa&utm_medium=referral';
if (is_array($myarray)) {
foreach ($myarray as $value) {
    if (is_array($value))
    {
        $x = findNeedle($value, $needle);
    }
}
5
  • Maybe describe what you want. Since the code is non-functional it doesn't tell us much. Commented Mar 23, 2017 at 18:48
  • If the code returns true if the match is found and a false if match is not found (in lp_domain key), it would work for me. Commented Mar 23, 2017 at 19:24
  • in_array($needle, array_column($myarray, 'lp_url')); returns true or false if matched or not. Commented Mar 23, 2017 at 19:29
  • Thanks. That works. Also this works: if(array_search($needle, array_column($myarray, 'lp_url')) !== False) { echo "FOUND"; } else { echo "Not Found"; } Commented Mar 23, 2017 at 19:43
  • Yes, that's a good one, but array_search returns a key so you have to compare with false as you have done. in_array returns true or false. Commented Mar 23, 2017 at 19:55

1 Answer 1

1

Extract an array of the data for the lp_url column and check for $needle:

if(in_array($needle, array_column($myarray, 'lp_url'))) {
    echo "Found";
} else {
    echo "Not found";
}
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.