0

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

2 Answers 2

1

I think you could use array_intersect to get the result you're looking for.

http://php.net/manual/en/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.

Result:

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

Try using this on your _source sub array.

Sign up to request clarification or add additional context in comments.

Comments

0

array_key_exists() is a search in the keys of the array, not in the values.

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;

3 Comments

This seems to be working! Give me a few moments to test it properly and I'll return.
@Ole just change the key of $result['source'] to $result['_source'] in "if()" statement, I wrote it wrongly.
Yes, I fixed that part and entered my table header and content. I'll try to come back later to post the entire code I used but for now I'll mark this answered because it seems to be working. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.