【问题标题】:How can I select that an item of array exists in other multidimensional array or not如何选择一个数组项是否存在于其他多维数组中
【发布时间】:2020-09-04 13:36:56
【问题描述】:

我有一个问题,我想查看一个多维数组的值(id_person)是否存在于其他多维数组中。 (id_person 是我想在这两个数组中比较的值 - Eddie Taylor: id_person 302 和 Jack Jones: id_person 311 exists in second array )。我希望有人可以帮助我。非常感谢。

Array one: ($homesquad)

array(4) {
  [0]=>
  array(9) {
    ["id"]=>
    string(3) "277"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(1) "8"
    ["id_person"]=>
    string(3) "306"
    ["name"]=>
    string(10) "Mark Jones"
    ["type"]=>
    string(1) "2"
    ["captain"]=>
    string(1) "0"
    ["pos"]=>
    string(1) "1"
  }
  [1]=>
  array(9) {
    ["id"]=>
    string(3) "282"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(2) "11"
    ["id_person"]=>
    string(3) "311"
    ["name"]=>
    string(10) "Jack Jones"
    ["type"]=>
    string(1) "2"
    ["captain"]=>
    string(1) "0"
    ["pos"]=>
    string(1) "4"
  }
  [2]=>
  array(9) {
    ["id"]=>
    string(3) "273"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(1) "4"
    ["id_person"]=>
    string(3) "302"
    ["name"]=>
    string(12) "Eddie Taylor"
    ["type"]=>
    string(1) "2"
    ["captain"]=>
    string(1) "0"
    ["pos"]=>
    string(1) "6"
  }
  [3]=>
  array(9) {
    ["id"]=>
    string(3) "270"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(1) "2"
    ["id_person"]=>
    string(3) "299"
    ["name"]=>
    string(13) "Jonas Haverla"
    ["type"]=>
    string(1) "2"
    ["captain"]=>
    string(1) "0"
    ["pos"]=>
    string(2) "10"
  }
}
Array two ($homeabsences)

array(5) {
  [0]=>
  array(7) {
    ["id"]=>
    string(3) "265"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(1) "1"
    ["id_person"]=>
    string(3) "294"
    ["name"]=>
    string(13) "Harry Jackson"
    ["type"]=>
    string(1) "1"
  }
  [1]=>
  array(7) {
    ["id"]=>
    string(3) "269"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(1) "2"
    ["id_person"]=>
    string(3) "311"
    ["name"]=>
    string(10) "Jack Jones"
    ["type"]=>
    string(1) "1"
  }
  [2]=>
  array(7) {
    ["id"]=>
    string(3) "288"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(2) "14"
    ["id_person"]=>
    string(3) "302"
    ["name"]=>
    string(12) "Eddie Taylor"
    ["type"]=>
    string(1) "1"
  }
  [3]=>
  array(7) {
    ["id"]=>
    string(3) "286"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(2) "14"
    ["id_person"]=>
    string(3) "315"
    ["name"]=>
    string(12) "Terry Barnes"
    ["type"]=>
    string(1) "2"
  }
  [4]=>
  array(7) {
    ["id"]=>
    string(3) "277"
    ["id_club"]=>
    string(2) "44"
    ["id_season"]=>
    string(1) "2"
    ["position"]=>
    string(1) "8"
    ["id_person"]=>
    string(3) "366"
    ["name"]=>
    string(14) "Jerry O'Donald"
    ["type"]=>
    string(1) "7"
  }
}

我当前的(更新的)代码(不起作用):

{foreach item=player from=$homesquad}
{if $player.pos == $count}  (filtering the first array based on pos value)

{if in_array("id_person",$homeabsences)}
Do X.
{else}
Do Y.
{/if}
{/if}
{/foreach}

【问题讨论】:

    标签: php arrays foreach smarty


    【解决方案1】:

    使用 PHP 函数 array_intersect 获取在第一个数组和第二个数组中找到的值。试试这个代码:

    <?php
    
    //First array
    $homesquad = array(
        array(
            "id" => "270",
            "id_club" => "44",
            "id_season" => "2",
            "position" => "8",
            "id_person" => "306",
            "name" => "Mark Jones",
            "type" => "2",
            "captain" => "0",
            "pos" => "1"
        ),
        array(
            "id" => "273",
            "id_club" => "44",
            "id_season" => "2",
            "position" => "4",
            "id_person" => "302",
            "name" => "Eddie Taylor",
            "type" => "2",
            "captain" => "0",
            "pos" => "6"
        )
    );
    
    //Second array
    $homeabsences = array(
        array(
            "id" => "277",
            "id_club" => "44",
            "id_season" => "2",
            "position" => "8",
            "id_person" => "366",
            "name" => "Jerry O'Donald",
            "type" => "2",
            "captain" => "0",
            "pos" => "1"
        ),
        array(
            "id" => "273",
            "id_club" => "44",
            "id_season" => "2",
            "position" => "4",
            "id_person" => "302",
            "name" => "Eddie Taylor",
            "type" => "2",
            "captain" => "0",
            "pos" => "6"
        )
    );
    
    //get intersection between $homesquad and $homeabsences
    $id_persons =  array_intersect(
        array_column($homesquad, 'id_person'), 
        array_column($homeabsences, 'id_person')
    );
    
    if($id_persons)
        print_r($id_persons); //this will display the intersection of two arrays
    else
        echo "no matches"
    ?>
    

    【讨论】:

    • 您好,感谢您的回答。我想只比较一个数组(玩家)而不是整个多维数组(homesquad)。 (foreach 循环的结果只是一个数组 - 一个玩家)。我想将该数组与 homeabsences 数组的 id_persons 进行比较。如果我用 $player 替换 $homesquad 那么它会起作用吗?
    • 所以最重要的是:foreach循环的结果只是一个数组,我想检查一下这个数组(玩家)的id_person在homeabsences(多维)数组中是否存在。跨度>
    【解决方案2】:

    要在数组中查找 id_person 的所有值,请使用 array_column 函数。然后你可以发现它是否重叠:

    array_intersect(
        array_column($homesquad, 'id_person'), 
        array_column($homeabsences, 'id_person')
    )
    

    或者对于单个值

    in_array($player['id_person'], array_column($homeabsences, 'id_person'))
    

    【讨论】:

    • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    相关资源
    最近更新 更多