-1
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)
{
...

我不知道如何循环它们。

4

3 回答 3

1

这应该为您打印数据。

foreach($child as $arr)
{

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

您可能希望添加适当的 HTML 标记来格式化它。

于 2012-12-02T06:39:16.830 回答
0
foreach($child as $arr)
{
   echo "output: ".$arr->tid." ".$arr->name;
}
于 2012-12-02T06:51:58.330 回答
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
        }
    }
}
于 2012-12-02T06:56:10.230 回答