array_filter不适用于多维数组

时间:2017-03-23 18:41:46

标签: php

这是非功能性代码。它没有返回任何东西。不太确定我使用的语法有什么问题。

 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);
    }
}

1 个答案:

答案 0 :(得分:1)

提取lp_url列的数据数组并检查$needle

if(in_array($needle, array_column($myarray, 'lp_url'))) {
    echo "Found";
} else {
    echo "Not found";
}