比较两个 stdClass 对象 [英] Comparing two stdClass Objects

查看:48
本文介绍了比较两个 stdClass 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Wordpress 主题中进行修改后的搜索,该搜索查询自定义分类法 location 并将其术语显示为搜索结果.我找不到内置的 Wordpress 函数来处理这个问题,所以我使用了 $wpdb 查询

I'm currently working on a modified search within a Wordpress theme that queries a custom taxonomy location and displays it's terms as search results. I couldn't find a built in Wordpress function to handle this, so I used a $wpdb query

$keywords = $_GET['s'];
$results = $wpdb->get_results( "SELECT * FROM $wpdb->terms WHERE name LIKE '%%$keywords%%'");

<ul>
<?php foreach ( $results as $result ) :?>
   <li><?php echo $result->name;?></li>
<?php endforeach;?>
</ul>

问题在于表 wp_terms 不仅存储自定义分类术语,还存储其他默认术语.因此,为了仅显示自定义分类法的搜索结果,而不是其他默认术语,我想到使用 get_terms 来获取属于自定义分类法的所有术语 location并根据 get_terms 结果和 in_array

The issue with this is the table wp_terms not only stores custom taxonomy terms, but other default terms as well. So in order to display search results just for the the custom taxonomy, not other default terms, I thought of using get_terms to get all of the terms belonging to the custom taxonomy location and display terms from the table wp_terms based off of get_terms result with in_array

$keywords = $_GET['s'];
$results = $wpdb->get_results( "SELECT * FROM $wpdb->terms WHERE name LIKE '%%$keywords%%'");

$terms = get_terms("location");

<ul>
<?php foreach ( $results as $result ) :?>
  if(in_array($result->name, $terms)) :?>
     <li><?php echo $result->name;?></li>
  <?php endif;?>
<?php endforeach;?>
</ul>

然而,$results$terms 都是 stdClass 对象,所以 in_array 不起作用.

However, $results and $terms are both stdClass Objects, so in_array doesn't work.

是否有函数、方法或可能的 MySQL 查询允许我根据对象 $terms 的内容显示来自对象 $results 的结果?

Is there either a function, method or possibly a MySQL query that will allow me to display results from the object $results based off the contents of the object $terms?

提前致谢.

$terms

Array ( 
  [0] => stdClass Object ( [term_id] => 32 [name] => US [slug] => us [term_group] => 0 [term_taxonomy_id] => 32 [taxonomy] => signs [description] => [parent] => 25 [count] => 1 ) 
  [1] => stdClass Object ( [term_id] => 22 [name] => EU [slug] => eu [term_group] => 0 [term_taxonomy_id] => 22 [taxonomy] => signs [description] => [parent]  => 0 [count] => 3 ) 
  [2] => stdClass Object ( [term_id] => 26 [name] => AU [slug] => au [term_group] => 0 [term_taxonomy_id] => 26 [taxonomy] => signs [description] => [parent] => 22 [count] => 1 ) 
  [3] => stdClass Object ( [term_id] => 27 [name] => IE [slug] => ie [term_group] => 0 [term_taxonomy_id] => 27 [taxonomy] => signs [description] => [parent] => 22 [count] => 2 ) 
  [4] => stdClass Object ( [term_id] => 23 [name] => PK [slug] => pk [term_group] => 0 [term_taxonomy_id] => 23 [taxonomy] => signs [description] => [parent] => 0 [count] => 2 ) 
)

推荐答案

您可以使用

(array) $variable;

或者,如果您的 stdClass 包含嵌套的 stdClass,您可以执行以下操作:

Or, if your stdClass contains nested stdClasses, you can do the following:

function obj_to_array_recursive(stdClass $obj) {
    foreach ($obj as &$element) {
        if ($element instanceof stdClass) {
            obj_to_array_recursive($element);
            $element = (array)$element;
        }
    }
    $obj = (array)$obj;
    return $obj;
}

这篇关于比较两个 stdClass 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆