无法搜索数组键=>另一个数组的值

时间:2013-08-05 18:34:54

标签: php arrays multidimensional-array associative-array

我正在尝试编写一个类/方法,它可以接收一个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!';
            }
        }

2 个答案:

答案 0 :(得分: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; 
            }
        }
    }
}

答案 1 :(得分: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';
}

UPDATE2

我认为这不起作用,因为你没有采取正确的价值观,: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,找到主键',如126所示:D