简体   繁体   English

PHP对象访问数组元素

[英]PHP object access array elements

I have this PHP object which I am trying to extract data from. 我有这个PHP对象,我试图从中提取数据。

Object: 宾语:

object(Symfony\Component\Test)#274 (2) {
  array(11) {
    ["objectclass"] array(4) {
      [0] "blue"
      [1] "yellow"
      [2] "red"
      [3] "green"
    }
    ["name"] array(1) {
      [0] "Bob"
    }
    ["surname"] array(1) {
      [0] "Peeterson"
    }
    ["title"] array(1) {
      [0] "Builder"
    }
    ["office"] array(1) {
      [0] "London-Branch"
    }
    ["givenname"] array(1) {
      [0] "Bob"
    }
    ["language"] array(1) {
      [0] "en-GB"
    }
    array(1) {
      [0] "565144652"
    }
    ["accounts"] array(2) {
      [0] "76474"
      [1] "16854"
    }
    array(1) {
      [0] "5"
    }
  }
}

So what I have here is a Object with 11 multidimensional arrays. 所以我在这里有一个带有11个多维数组的Object。 Now lets say now I only need to get all the elements from objectclass array && name array && accounts array 现在让我们说现在我只需要从objectclass array && name array && accounts array获取所有元素

Ok so this is how i have tried to do this but get absolutely nothing: 好的,这就是我试图做到这一点,但绝对没有:

OK so the object is stored in a var ie: 好的,所以对象存储在var中:

$data = object();
foreach($data as $usr)
{
   var_dump($usr->objectclass);
}

You are looping an object getting each of the elements on it. 您正在循环一个对象,获取其中的每个元素。 What would seem more appropriate is to just call what you want 看起来更合适的是打电话给你想要的东西

$objectclass = $data->objectclass; //this is an array you can loop or access 
$objectclass[0]; //blue

Same for the other portion. 其他部分也是如此。

$name = $data->name[0]; //Bob

Of these you get arrays which you could loop 其中你得到的数组可以循环

foreach ($objectclass as $oc){
    echo $oc;
}

output: 输出:

blue 蓝色

yellow 黄色

red 红色

green 绿色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM