PHP检查另一个数组中是否存在一个数组中的值

时间:2018-08-29 14:09:49

标签: php html

我的项目中有两个数组。它们是“ $ results”和“ $ hits_navn”。

$ hits_navn看起来像这样:

Array ( [0] => HOPLAND ROGER [1] => TRACKSON AS ) 

$ results看起来像这样:

array(4) {
  [0]=>
  array(5) {
    ["_index"]=>
    string(19) "aksjeregisteret2017"
    ["_type"]=>
    string(3) "doc"
    ["_id"]=>
    string(20) "vBDH72QBa47gCFuEn-e4"
    ["_score"]=>
    float(168.98682)
    ["_source"]=>
    array(14) {
      ["Antall Aksjer"]=>
      string(2) "50"
      ["host"]=>
      string(2) "dr"
      ["message"]=>
      string(78) "998814308;TRACKSON AS;Ordinære aksjer;SKEIDE ROALD;1973;3047;DRAMMEN;50;1000
"
      ["@timestamp"]=>
      string(24) "2018-07-31T09:59:54.203Z"
      ["Poststed"]=>
      string(7) "DRAMMEN"
      ["Selskap"]=>
      string(11) "TRACKSON AS"
      ["Navn Aksjoner"]=>
      string(12) "SKEIDE ROALD"
      ["Orgnr"]=>
      string(9) "998814308"
      ["Postnr"]=>
      string(4) "3047"
      ["Antall Aksjer Selskap"]=>
      string(4) "1000"
      ["@version"]=>
      string(1) "1"
      ["Fodselar/Orgnr"]=>
      string(4) "1973"
      ["Aksjeklasse"]=>
      string(16) "Ordinære aksjer"
      ["path"]=>
      string(44) "/home/ole/Nedlastinger/Aksjeeierbok22017.csv"
    }
  }
  [1]=>
  array(5) {
    ["_index"]=>
    string(19) "aksjeregisteret2017"
    ["_type"]=>
    string(3) "doc"
    ["_id"]=>
    string(20) "4ge772QBa47gCFuEh2bs"
    ["_score"]=>
    float(151.22754)
    ["_source"]=>
    array(14) {
      ["Antall Aksjer"]=>
      string(4) "1980"
      ["host"]=>
      string(2) "dr"
      ["message"]=>
      string(80) "980645525;ETRINELL AS;NO0010022411;SKEIDE ROALD;1973;3047;DRAMMEN;1980;42078314
"
      ["@timestamp"]=>
      string(24) "2018-07-31T09:46:41.674Z"
      ["Poststed"]=>
      string(7) "DRAMMEN"
      ["Selskap"]=>
      string(11) "ETRINELL AS"
      ["Navn Aksjoner"]=>
      string(12) "SKEIDE ROALD"
      ["Orgnr"]=>
      string(9) "980645525"
      ["Postnr"]=>
      string(4) "3047"
      ["Antall Aksjer Selskap"]=>
      string(8) "42078314"
      ["@version"]=>
      string(1) "1"
      ["Fodselar/Orgnr"]=>
      string(4) "1973"
      ["Aksjeklasse"]=>
      string(12) "NO0010022411"
      ["path"]=>
      string(44) "/home/ole/Nedlastinger/Aksjeeierbok22017.csv"
    }
  }
  [2]=>
  array(5) {
    ["_index"]=>
    string(19) "aksjeregisteret2017"
    ["_type"]=>
    string(3) "doc"
    ["_id"]=>
    string(20) "pw3C72QBa47gCFuE4jau"
    ["_score"]=>
    float(145.292)
    ["_source"]=>
    array(14) {
      ["Antall Aksjer"]=>
      string(2) "10"
      ["host"]=>
      string(2) "dr"
      ["message"]=>
      string(70) "991715649;GOLDENWOOD AS;A-aksjer;SKEIDE ROALD;1973;3047;DRAMMEN;10;10
"
      ["@timestamp"]=>
      string(24) "2018-07-31T09:54:43.517Z"
      ["Poststed"]=>
      string(7) "DRAMMEN"
      ["Selskap"]=>
      string(13) "GOLDENWOOD AS"
      ["Navn Aksjoner"]=>
      string(12) "SKEIDE ROALD"
      ["Orgnr"]=>
      string(9) "991715649"
      ["Postnr"]=>
      string(4) "3047"
      ["Antall Aksjer Selskap"]=>
      string(2) "10"
      ["@version"]=>
      string(1) "1"
      ["Fodselar/Orgnr"]=>
      string(4) "1973"
      ["Aksjeklasse"]=>
      string(8) "A-aksjer"
      ["path"]=>
      string(44) "/home/ole/Nedlastinger/Aksjeeierbok22017.csv"
    }
  }
  [3]=>
  array(5) {
    ["_index"]=>
    string(19) "aksjeregisteret2017"
    ["_type"]=>
    string(3) "doc"
    ["_id"]=>
    string(20) "xQq_72QBa47gCFuE49kk"
    ["_score"]=>
    float(139.8048)
    ["_source"]=>
    array(14) {
      ["Antall Aksjer"]=>
      string(3) "300"
      ["host"]=>
      string(2) "dr"
      ["message"]=>
      string(92) "987199458;REVISORGRUPPEN DRAMMEN AS;Ordinære aksjer;SKEIDE ROALD;1973;3047;DRAMMEN;300;300
"
      ["@timestamp"]=>
      string(24) "2018-07-31T09:51:27.213Z"
      ["Poststed"]=>
      string(7) "DRAMMEN"
      ["Selskap"]=>
      string(25) "REVISORGRUPPEN DRAMMEN AS"
      ["Navn Aksjoner"]=>
      string(12) "SKEIDE ROALD"
      ["Orgnr"]=>
      string(9) "987199458"
      ["Postnr"]=>
      string(4) "3047"
      ["Antall Aksjer Selskap"]=>
      string(3) "300"
      ["@version"]=>
      string(1) "1"
      ["Fodselar/Orgnr"]=>
      string(4) "1973"
      ["Aksjeklasse"]=>
      string(16) "Ordinære aksjer"
      ["path"]=>
      string(44) "/home/ole/Nedlastinger/Aksjeeierbok22017.csv"
    }
  }
}

我想做的是检查$ hits_navn中的值是否存在于$ results的一个或多个索引([0]或[1]等)中。如果它们确实存在(又名匹配),我想在页面的html表中输入它们。

我在获取它时遇到了问题。我尝试在一段时间内使用if(array_key_exists),但是这样做没有任何结果。我尝试添加一个foreach($ results作为$ results),但这会给我以下错误:

Warning: array_key_exists() expects parameter 2 to be array, string given in /var/www/html/altinn/index.php on line 195

四次。

我显然做错了什么。有没有做这种事情的正确方法?

我正在使用的代码:

$tal = 0;
while ($tal < count($hits_navn)) {
    foreach ($results as $results) {
        if (array_key_exists($hits_navn[$tal], $results['_source'])) {

        //Creates the first row of the table.
        if (!isset($it)) {
        echo "<div class='container'>
        <div class='table-responsive'>
        <table class='table table-hover'>
        <thead class='thead-light'>
        <tr>
        <th scope='col'>Orgnr</th>
        <th scope='col'>Selskap</th>
        <th scope='col'>Navn Aksjoner</th>
        <th scope='col'>Fodselar/Orgnr</th>
        <th scope='col'>Antall Aksjer</th>
        <th scope='col'>Antall Aksjer Selskap</th>
        </tr>
        </thead>";
        $it = 1;
        }

        //Creates all other rows in the table.
        <tr>
        <td><?php echo $results['_source']['Orgnr'] ?></td>
        <td><?php echo $results['_source']['Selskap'] ?></td>
        <td><?php echo $results['_source']['Navn Aksjoner'] ?></td>
        <td><?php echo $results['_source']['Fodselar/Orgnr'] ?></td>
        <td><?php echo $results['_source']['Antall Aksjer'] ?></td>
        <td><?php echo $results['_source']['Antall Aksjer Selskap'] ?></td>
        </tr>
        <?php
        }
    }
    $tal++;
} 

2 个答案:

答案 0 :(得分:0)

我认为您可以使用array_intersect来获得所需的结果。

http://php.net/manual/en/function.array-intersect.php

示例:

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);

如您所见,这完全忽略了array key并只寻找值。

结果:

Array
(
    [a] => green
    [0] => red
)

尝试在_source子数组上使用它。

答案 1 :(得分:-1)

array_key_exists()是对数组键而不是值的搜索。

您可以做什么:

<link href=...>