简体   繁体   中英

PHP object access array elements

I have this PHP object which I am trying to extract data from.

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. Now lets say now I only need to get all the elements from 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:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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