I've two arrays in my project. They are "$results" and "$hits_navn".
$hits_navn looks like this:
Array ( [0] => HOPLAND ROGER [1] => TRACKSON AS )
While $results looks like this:
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. If they do exist (aka there is a match) I want to enter them into a html table on the page.
I'm having problem getting it. I've tried using if(array_key_exists) inside a while but doing that gives me no results. I've tried adding a foreach($results as $results) but this gives me the following errors:
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++;
}