如何从php中的数组输出中获取变量值

时间:2017-11-08 07:27:47

标签: php arrays

我使用print_r输出如下所示 现在我想在变量中保存一个特定的值供以后使用。下面是数组格式的输出。

Array 
 (
[count] => 1 [0] => 
Array ( [objectclass] => 
Array ( [count] => 4 [0] => top [1] => person [2] => organizationalPerson 
[3] => user ) 
[0] => objectclass [cn] => Array ( [count] => 1 [0] => Tomas Alva ) 
[1] => cn [title] => Array ( [count] => 1 [0] => Assistant Director )
)
)

我希望得到价值" Tomas Alva"和#34;助理总监"在两个变量中。

我怎么能得到这个。

2 个答案:

答案 0 :(得分:0)

那么你可以像$variable[0]['cn'][0]那样接受它。但这对以后的使用来说不太正确。

答案 1 :(得分:0)

$arr = Array 
    (
        [count] => 1 [0] => 
            Array ( 
                [objectclass] => Array ( 
                        [count] => 4 
                        [0] => top 
                        [1] => person 
                        [2] =>  organizationalPerson 
                        [3] => user 
                ) 
                [0] => objectclass 
                [cn] => Array ( 
                    [count] => 1 
                    [0] => Tomas Alva 
                ) 
                [1] => cn 
                [title] => Array ( 
                    [count] => 1 
                    [0] => Assistant Director 
                )
          )
   );

$name = $arr['count']['cn']['0'];
$position = $arr['count']['title']['0'];
顺便说一句,你的数组很奇怪。为什么将指定的指数混合在一起? count, 0, 1, ...