PHP:从一组数组中检索数据


PHP : Retrive data from a set of arrays

我有一组数据,看起来像这个

 object(stdClass)#5 (39) { ["id"]=> int(125273716) ["status"]=> object(stdClass)#6 (18) { ["retweeted"]=> ["text"]=> string(28) "1234567" } ["is_translator"]=> bool(false)}

如何获取["text"]我删除了数据的某些部分,因为它太长了。我只需要['text']参数。感谢

试试这个:

$object->status->text

将stdClass对象转换为多维数组的函数

<?php
    function objectToArray($d) {
        if (is_object($d)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $d = get_object_vars($d);
        }
        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map(__FUNCTION__, $d);
        }
        else {
            // Return array
            return $d;
        }
    }
?>

用途:

<?php
echo '<pre>';
var_dump(objectToArray($object));
echo '</pre>';

来源:http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/

这是一个对象

echo $result->status->text;

object(stdClass)#5(39){["id"]=>int(125273716)["status"]=>object(stdClass)#6(18){"retweeted"]=>["text"]=>string(28)"1234567"}["is_translator"]=>bool(false)}

您可以尝试将$res作为

$res = $result->status->text;
echo $res;