按值访问数组元素

时间:2012-06-01 09:36:28

标签: php

我有一个像:

这样的数组
stdClass Object
(
    [223] => Array
        (

            [0] => stdClass Object
                (
                    [tribeName] => TTTT
                    [tribeid] => 31
                    [numusers] => 10
                    [dateCreated] => 2012-05-28 18:30:34
                )

            [1] => stdClass Object
                (
                    [tribeName] => KKKKK
                    [tribeid] => 32
                    [numusers] => 11
                    [dateCreated] => 2012-05-28 18:32:12
                )

            [2] => stdClass Object
                (
                    [tribeName] => RRRR
                    [tribeid] => 33
                    [numusers] => 5
                    [dateCreated] => 2012-05-28 18:53:17
                )

            [3] => stdClass Object
                (
                    [tribeName] => mytehhhhhst2
                    [tribeid] => 34
                    [numusers] => 4
                    [dateCreated] => 2012-05-28 18:56:15
                )

            [4] => stdClass Object
                (
                    [tribeName] => yog
                    [tribeid] => 58
                    [numusers] => 1
                    [dateCreated] => 2012-06-01 12:32:47
                )

            [5] => stdClass Object
                (
                    [tribeName] => newFunc
                    [tribeid] => 63
                    [numusers] => 1
                    [dateCreated] => 2012-06-01 12:45:01
                )

            [6] => stdClass Object
                (
                    [tribeName] => Jghj
                    [numusers] => 0
                    [tribeid] => 
                )
        )
)

这里,223是关键值,每次都是新的。我将其作为密钥($ array)访问。我会得到223值。 但是如何访问它的值,例如tribeName?请帮助我。

感谢。

3 个答案:

答案 0 :(得分:2)

试试这个

  foreach ($array as $value) {
           foreach($value as $data )
               echo $data->tribeName;
    }

答案 1 :(得分:0)

AFAIK,您必须遍历数组并手动检查每个元素。 当然,你也可以为此编写一个函数。

如果您需要比较整个数组元素,而不仅仅是其成员,则可以使用array_search来确定密钥。

答案 2 :(得分:0)

试试这个..

foreach($var as $key => $val)
    {
            $kkv = $var[$key];
            $inarray = $kkv[0];
            print_r($inarray);
            echo $inarray['tribeName'];
    }