2

我似乎对此有点迷茫,我试图解析一些信息,但 stdClass 总是会发生变化,所以我不太确定该怎么做,可以使用来指导。

//询问

$query = new EntityFieldQuery;
$result = $query
  ->entityCondition('entity_type', 'taxonomy_term')
  ->propertyCondition('name', 'GOOG')
  ->propertyCondition('vid', '3')
  ->execute();

//这是输出

Array
(
    [taxonomy_term] => Array
        (
            [1868] => stdClass Object
                (
                    [tid] => 1868
                )

        )

)

现在我可以使用 tid

$result['taxonomy_term']['1868']->tid

但如前所述,stdClass 将始终在变化。

4

1 回答 1

2

您可以像这样使用递归数组搜索:

function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() )
{
    if( !is_array($haystack) ) {
        return false;
    }

    foreach( $haystack as $key => $val ) {
        if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
            $path = array_merge($path, array($key), $subPath);
            return $path;
        } elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
            $path[] = $key;
            return $path;
        }
    }
    return false;
}

用法:

$arr = (array) $yourObject;
$keypath = array_searchRecursive('tid', $arr);

例子:

$class = new stdClass;
$class->foo = 'foo';
$class->bar = 'bar';
$arr = (array) $class;
$keypath = array_searchRecursive('foo', $arr);
print_r($keypath);

结果:

Array
(
    [0] => foo
)

所以现在要获得实际价值:

echo $keypath[0]; // foo
于 2012-07-01T19:03:35.323 回答