如何输出结果?

时间:2012-12-02 06:33:04

标签: php

Array
(
[9] => stdClass Object
    (
        [tid] => 9
        [vid] => 3
        [name] => Domestic 
        [description] => Domestic 
        [weight] => 0
    )

[12] => stdClass Object
    (
        [tid] => 12
        [vid] => 3
        [name] => Economic 
        [description] => Economic 
        [weight] => 1
    )

[11] => stdClass Object
    (
        [tid] => 11
        [vid] => 3
        [name] => International
        [description] => 
        [weight] => 2
    )


 )
假设print_r($ child)的结果是上面的,我想如何回显所有的tid和名字,我该怎么办?

foreach($child as $arr){
 foreach($arr as $key=>$vaule)
{
...

我不知道如何循环它们。

3 个答案:

答案 0 :(得分:1)

这应该为您打印数据。

foreach($child as $arr)
{

   echo 'Tid: ' . $arr->tid . ' and Name: ' . $arr->name . "\n";
}

您可能希望添加适当的HTML标记以对其进行格式化。

答案 1 :(得分:0)

foreach($child as $arr)
{
   echo "output: ".$arr->tid." ".$arr->name;
}

答案 2 :(得分:0)

您可以使用此php函数

将对象转换为数组
<?PHP

function object2array($data)
{
    if (is_array($data) || is_object($data))
    {
        $result = array();
        foreach ($data as $key => $value)
        {
            $result[$key] = object2array($value);
        }
        return $result;
    }
    return $data;
}

?>

使用此功能后,您可以使用此代码。

foreach($child as $arr)
{
    foreach($arr as $key=>$val)
    {
        if($key == "tid" || $key == "name")
        {
            // Here some code
        }
    }
}