1

I'm working on something which requires me to get an attribute from an array, Which I thought was fairly simple. Weirdly I can't figure out how to get the attribute. The attributes that I need to achieve is the IP address in an array. If I dumpDie the object it returns the following

0 => Networks{#1010 ▼
  +ipAddress: "192.125.3.232"
  +gateway: "192.125.0.1"
  +type: "public"
  etc......

Now I need to receive the IP address from this array. How can I achieve that?

I've tried doing things like

$data['networks'][0]['ipAddress']; or $data->{'networks[0]'}->{'ipAddress'}

But both of them gave the error

Cannot use object of type DigitalOceanV2\Entity\Droplet as array

What is the solution to this problem?

EDIT

I get the object doing this

$droplet = DigitalOcean::droplet()->create($storeName, 'ams3', 's-1vcpu-1gb', $images[0]->id);

$data = DigitalOcean::droplet()->getById($droplet->id);
8
  • 2
    Networks is capitalized. Commented Apr 9, 2018 at 12:16
  • @Adam That doesn't make a difference otherwise I wouldn't have posted the question. I've tried that already. Commented Apr 9, 2018 at 12:18
  • Can you give us a complete dump of the array in question then? Commented Apr 9, 2018 at 12:19
  • @SougataBose Unfortunately that does not work. It gives me the same error stated above. Commented Apr 9, 2018 at 12:22
  • @Adam The complete dump is this imgur.com/a/QUfTa Commented Apr 9, 2018 at 12:25

2 Answers 2

1

Try this code, First check that you have values in $networks by,

$networks = $data['networks'];

And if $networks is not empty, Try this

$ipAddress = $networks[0]['ipAddress'];
Sign up to request clarification or add additional context in comments.

4 Comments

That gives me the error Cannot use object of type DigitalOceanV2\Entity\Droplet as array. I don't understand this at all
Can you use this $data = DigitalOcean::droplet()->find($droplet->id); instead of using getById ? and check the result it's coming or not?
It gives me the same error as the first comment on this question
0

But both of them gave the error

Cannot use object of type DigitalOceanV2\Entity\Droplet as array

Because Droplet is an object, so you need to use -> to get the properties.

$data->networks[0]->ipAddress

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.