Projektnummer Projek" /> Projektnummer Projek" /> Projektnummer Projek"/>

array_diff dont work correctly. How to solve this issue?

123 Views Asked by At

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?

1

There are 1 best solutions below

1
AudioBubble On BEST ANSWER

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