0

我正在尝试编写一个可以接收 id 数组并在主数组键中搜索 id 的类/方法。如果找到密钥,则搜索内部数组并确保不存在其他 ID。

例子:

$ids = array(120, 123, 456, 789);

$primary[120] = array(456,888,99);

$primary[111] = array(789, 886,120);

输出会说 ID-120 无效,因为存在 ID-456。

我已经考虑过做一个 foreach ,但它对我来说有点太复杂了,我觉得必须有一种更简单的方法来做到这一点?也许看 array_walk 但没有运气使它工作。

这是我正在尝试的,但它不起作用......

$ids = array(125,126,182);
    $primary = array();
    $primary['125'] = array(126,301,302,403);

        foreach ($primary as $keyP => $valueP) 
        {
            if(in_array($keyP, $ids)
            {
            echo 'Woops, Primary key found!';
            }
        }
4

2 回答 2

3

我认为最简单的方法是使用 foreach 循环(嵌套它们)遍历每个数组,然后使用 in_array 函数评估它是否存在。以下代码是您可以执行的操作的示例:

$ids = array(120, 123, 456, 789,99);

$primary[120] = array(456,888,99);

$primary[111] = array(789, 886,120);

function validateArray($keys, $vals)
{
    foreach ($keys as $key)
    {
        if (isset($vals[$key]))
        {
            foreach ($vals[$key] as $checkId)
            {
                if ( in_array($checkId, $keys) )
                {
                    echo "ID-".$key." is invalid because ID-".$checkId." is present.";
                    // Uncomment if you want to stop evaluating after the first invalidation
                    //return false; 
                }
            }
        }
    }
}
validateArray($ids, $primary);

ID-120 is invalid because ID-456 is present.当我测试这个函数时,我得到了结果。让我知道这是否适合您,或者您对我的功能如何工作有任何疑问:)

版本 2

使用array_intersect().. 消除了 for 循环,更重要的是,消除了in_array().

function validateArray($keys, $vals)
{
    foreach ($keys as $key)
    {
        if (isset($vals[$key]))
        {
            $common = array_intersect($keys, $vals[$key]);
            if ( count($common) > 0)
            {
                echo "ID-".$key." is invalid because ID-".array_shift($common)." is present.";
                // Uncomment if you want to stop evaluating after the first invalidation
                ///return false; 
            }
        }
    }
}
于 2013-08-05T19:04:08.093 回答
0

嗯,使用 in_array() 函数怎么样?

如果不在 ids 数组中,则仅分配值:

foreach ( $primary as $keyP => $valueP ) {
    if ( in_array( $keyP, $ids) {
        echo 'Woops, Primary key found!';
    }
}

那么,上面的方法不起作用,那怎么办?

$ids = array(1, 5, 10);

$flipIds = array_flip($ids);

print_r( $flipIds );

// This ids are ok, so, it shows the position in the original array
echo $flipIds[1]; //  OK, 0 as answer
echo $flipIds[5]; //  OK, 1 as answer
echo $flipIds[10]; // OK, 2 as answer

// Dirty Trick, don't show Notice!
// This shows 'Waps' because it's not in the array (originally a Notice, :P)
if ( @$flipIds[2] ) {
    echo 'ok';
} else {
    echo 'waps';
}

更新2

我认为它不起作用,因为您没有采用正确的价值观,:P。尝试这个:

$ids = array(125,126,182);
$primary = array();
$primary['125'] = array(126,301,302,403);

foreach ($primary as $keyP => $valueP) {
    if ( in_array( $keyP, $ids ) {
            echo 'Woops, key From $primary is a Primary key!';
    }
    foreach ($valueP as $id ) {
        if ( in_array($id, $ids) ) {
            echo 'Woops, Primary key found!';
        }
    }
}

这将输出 'Woops, Primary key found',因为 126 在那里,:D

于 2013-08-05T18:38:18.083 回答