如何检查两个数组合并以及是否存在值以添加新密钥

时间:2014-12-29 13:08:42

标签: php arrays

$ now = check key

$ all =所有数据

我需要检查一些id在数组$ all

$now = array(13,15,18);

$all = 
Array(
    [0] => Array
        (
            [lid] => 4
            [name] => aaaaa
        )

    [1] => Array
        (
            [lid] => 12
            [name] => s
        )

    [2] => Array
        (
            [lid] => 13
            [name] => aaa
        )

    [3] => Array
        (
            [lid] => 14
            [name] => bbb
        )

    [4] => Array
        (
            [lid] => 15
            [name] => ccc
        )

    [5] => Array
        (
            [lid] => 16
            [name] => ddd
        )

    [6] => Array
        (
            [lid] => 17
            [name] => eee
        )

    [7] => Array
        (
            [lid] => 18
            [name] => fffff
        )

)

$现在价值是盖子, 我现在需要用$来检查$ all ...

$现在是13,15,18

$所有有8行盖子:4,12,13,14,15,17,18

13,15,18在数组中

所以我需要添加一个新密钥并将值ext = 1。

设置为:

Array(
    [0] => Array
        (
            [lid] => 4
            [name] => aaaaa
            [ext] => 0
        )

    [1] => Array
        (
            [lid] => 12
            [name] => s
            [ext] => 0
        )

    [2] => Array
        (
            [lid] => 13
            [name] => aaa
            [ext] => 1
        )

    [3] => Array
        (
            [lid] => 14
            [name] => bbb
            [ext] => 0
        )

    [4] => Array
        (
            [lid] => 15
            [name] => ccc
            [ext] => 1
        )

    [5] => Array
        (
            [lid] => 16
            [name] => ddd
            [ext] => 0
        )

    [6] => Array
        (
            [lid] => 17
            [name] => eee
            [ext] => 0
        )

    [7] => Array
        (
            [lid] => 18
            [name] => fffff
            [ext] => 1
        )

)

4 个答案:

答案 0 :(得分:1)

您应该循环遍历$all数组,如下所示:

foreach ($all as &$item) {
    $item['ext'] = (in_array($item['lid'], $now) ? 1 : 0);
}

答案 1 :(得分:1)

以下是您可以尝试的内容:

   <?php
           $now = array(13,15,18);

           $all = array(array('lid'=>4,'name'=>'aaaa'),array('lid'=>12,'name'=>'s'),array('lid'=>13,'name'=>'aaaa'),array('lid'=>14,'name'=>'bbb'),array('lid'=>15,'name'=>'ccc'),array('lid'=>16,'name'=>'ddd'),array('lid'=>17,'name'=>'eeee'),array('lid'=>18,'name'=>'ffff'));


           //echo"<pre>";print_r($all);
           $i = 0;
           foreach($all as $a){
             if (in_array($a['lid'], $now))
                {
                  $all[$i]['ext'] = 1;
                }
             else
                {
                  $all[$i]['ext'] = 0;
                }
             $i++;

           }
           echo"<pre>";print_r($all);


  ?>

答案 2 :(得分:0)

您需要执行以下操作:

$now = array(12,14);

$all = array(
    array(
            "lid" => 4,
            "name" => "aaaaa"
        ),

    array(
            "lid" => 12,
            "name" => "s"
        ),

    array(
            "lid" => 13,
            "name" => "aaa"
        ),

    array(
            "lid" => 14,
            "name" => "bbb"
        )
        );
$i = 0;
foreach ( $all as $each ) {
    if ( in_array($each['lid'],$now) ) {
        $all[$i]['ext'] = 1;
    } else {
        $all[$i]['ext'] = 0;
    }
    $i++;
}
print_r($all);

答案 3 :(得分:0)

试试这个

foreach($all AS $key => $a) {

    $all1[$key] = $a;

    $all1[$key]['ext'] = 0;

    if(in_array($a['lid'], $now)) {
        $all1[$key]['ext'] = 1;
    }

}

的print_r($ ALL1);

的print_r($全部);