简体   繁体   English

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

[英]PHP Check if an value in an array exists in another array

I've two arrays in my project. 我的项目中有两个数组。 They are "$results" and "$hits_navn". 它们是“ $ results”和“ $ hits_navn”。

$hits_navn looks like this: $ hits_navn看起来像这样:

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

While $results looks like this: 虽然$ 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"
    }
  }
}

What I'm trying to do is check if the values inside $hits_navn exists inside one or more of the indexes (the [0], or [1], etc) of $results. 我想做的是检查$ hits_navn内部的值是否存在于$ results的一个或多个索引([0]或[1]等)中。 If they do exist (aka there is a match) I want to enter them into a html table on the page. 如果它们确实存在(又名匹配),我想在页面的html表中输入它们。

I'm having problem getting it. 我在获取它时遇到问题。 I've tried using if(array_key_exists) inside a while but doing that gives me no results. 我试过一段时间内使用if(array_key_exists),但这样做没有任何结果。 I've tried adding a foreach($results as $results) but this gives me the following errors: 我尝试添加一个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

Four times. 四次。

I'm obviously doing something very wrong. 我显然做错了什么。 Is there a proper way to do something like this? 有适当的方法来做这样的事情吗?

The code i'm working with: 我正在使用的代码:

$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++;
} 

I think you could use array_intersect to get the result you're looking for. 我认为您可以使用array_intersect获得想要的结果。

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

Example: 例:

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

As you can see this completely ignores the array key and just looks for the value. 如您所见,这完全忽略了array key ,只寻找值。

Result: 结果:

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

Try using this on your _source sub array. 尝试在_source子数组上使用它。

array_key_exists() is a search in the keys of the array, not in the values. array_key_exists()是在数组的键中搜索,而不是在值中搜索。

Here what you can do: 您可以在这里做什么:

$tableHeader = "[header]"; // put the header of the table here
$tableContent = "";

foreach ($results as $result) {
    foreach ($hits_navn as $navn) {
        // found in results array
        if (in_array($navn, $result) or in_array($navn, $result['source')) {
            $tableContent .= "[row content]"; // create a row for the table
        }
    }
}

$table = $tableHeader . $tableContent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM