0

Hey i have the following code:

<html>
<table>
  <table>
  <thead align="left" style="display: table-header-group">
  <tr>
     <th>Projektnummer </th>
     <th>Projektbezeichnung</th>
  </tr>
  </thead>
<tbody>
<?php 
    global $wpdb;
    $result = json_decode(json_encode($wpdb->get_results("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_field_projekt-laufend'")),true);
    $result2 = json_decode(json_encode($wpdb->get_results("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_field_projekt-abgeschlossen'")),true);

    $finishedresult = array_diff($result, $result2);

    $data['items'] = $finishedresult;
$total = 0;
foreach ($data['items'] as $rows) :?>
  <tr class="item_row">
        <td><?php echo ++$total; ?></td>
        <td> <?php echo $rows['meta_value']; ?></td>

  </tr>
<?php endforeach;?>
</tbody>
</table>

</html>

The SELECT meta_value FROM wp_postmeta WHERE meta_key = '_field_projekt-laufend' returns:

Das Erste Projekt

Das Zweite Projekt

The SELECT meta_value FROM wp_postmeta WHERE meta_key = '_field_projekt-abgeschlossen' returns:

Das Erste Projekt

Now the array_diff should compare and give me the result: "Das Zweite Projekt"

But after the array_diff the array $finishedresult is empty. That cant be one array contains two different values and the other array contains 1 value. So there is no way that it can be empty.

Any Idea how to solve this problem?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • What have you tried to resolve the problem? Where are you stuck? Please reduce that problem to the relevant parts: is this **really** related to Wordpress or a database? – Nico Haase Feb 15 '22 at 07:08
  • 2
    If you share `$result` and `$result2` and remove all the rest from your code, it would be way easier to help spot the problem – Nico Haase Feb 15 '22 at 07:09
  • 3
    I wouldn't accuse `array_diff()` of not working correctly, yet. Perhaps it would be better to say that it doesn't do what you expect. To see why the result is empty we only need the two inputs, then we can tell you. Can you provide those? See: [var_export()](https://www.php.net/manual/en/function.var-export.php) for a good method to produce them. Do not simply type in what you think `$result1` and `$result2` are. Humans are notoriously bad at copying things accurately. – KIKO Software Feb 15 '22 at 07:17

1 Answers1

0

Here is the new code it contains at first the version with the problem and at second the solution. array_diff cant handle the SQL query results so inserted the values in a new string array and now it works.

<html>
<table>
  <table>
  <thead align="left" style="display: table-header-group">
  <tr>
     <th>Projektnummer </th>
     <th>Projektbezeichnung</th>
  </tr>
  </thead>
<tbody>
<?php 
    global $wpdb;
    $SQLQueryResult = json_decode(json_encode($wpdb->get_results("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_field_projekt-laufend'")),true);
    $SQLQueryResult2 = json_decode(json_encode($wpdb->get_results("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_field_projekt-abgeschlossen'")),true);

    var_export($SQLQueryResult);
    var_export($SQLQueryResult2);
    $diffArray = array_diff($SQLQueryResult, $SQLQueryResult2);
    var_export($diffArray);

    $result1array = [];
    $data['items'] = $SQLQueryResult;
    foreach ($data['items'] as $rows) :
        array_push($result1array, $rows['meta_value']);
    endforeach;
    var_export($result1array);
    
    $result2array = [];
    $data['items'] = $SQLQueryResult2;
    foreach ($data['items'] as $rows) :
        array_push($result2array, $rows['meta_value']);
    endforeach;
    var_export($result2array);
    
    $diffArray = array_diff($result1array, $result2array);
    var_export($diffArray);


$total = 0;
foreach ($diffArray as &$item) :?>
  <tr class="item_row">
        <td><?php echo ++$total; ?></td>
        <td> <?php echo $item; ?></td>

  </tr>
<?php endforeach;?>
</tbody>
</table>

</html>

Here the output of the page: enter image description here

S.I.
  • 3,250
  • 12
  • 48
  • 77
  • 3
    Does this answer your question or are you trying to provide more information? If it's the latter then please [update](https://stackoverflow.com/posts/71122309/edit) your question with the new (relevant) information. – DarkBee Feb 15 '22 at 15:39