2

i have a multidimensional array the first two statements of code work fine... notice them below...

      echo $arrayObjects['name'].'<br>';
      echo $arrayObjects['ipv4']['10.14.2.22']['type'].'<br>';

but i want to be able to return the keys in the ipv4 array, instead of having to hard code them like i did above. when i do the following:

      print_r( array_keys($arrayObjects['ipv4'])); 

I get the following output:

         Array ( [0] => 10.14.2.22 ) 

which is great. but how do i get the ip address returned to me as a variable?

1
  • $ips = array_keys($arrayObjects['ipv4']); $ip = $ips[0]; var_dump($ip); Commented Aug 10, 2012 at 15:49

4 Answers 4

1

Try:

First Element:

$ip_address = array_shift(array_keys($arrayObjects['ipv4']));

Each Element:

foreach($arrayObjects['ipv4'] as $ip_address => $value){
    print_r($ip_address);
}
Sign up to request clarification or add additional context in comments.

Comments

0
foreach( $arrayObjects['ipv4'] as $ip => $content )
{
 echo $ip;
}

Comments

0

Use:

current(array_keys($arrayObjects['ipv4']));

This simply extracts the first element out of any array when used like this.

The value returned from this should be '10.14.2.22'

http://php.net/manual/en/function.current.php

Comments

0

You would do this in a foreach loop:

foreach ($arrayObjects['ipv4'] as $key => $value)
{
    // Echo out the key OR save it to another variable..
    echo $key;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.