jam
jam

Reputation: 65

Get certain value from array

I have an array

Array ( 
  [0] => 
  [12] => Array ( 
    [termimages] => Array ( 
      [0] => 58
      [1] => 57
      [2] => 56 
    ) 
  )
)

My result with print_r from $meta.

How can I set a value "12" from array to variable?

Thanks in advance!

Upvotes: 1

Views: 106

Answers (3)

Esailija
Esailija

Reputation: 140210

How can I set a value "12" from array to variable?

Since there is no value "12" anywhere, I assume you mean the key "12", which has the value of:

Array ( 
    [termimages] => Array ( 
      [0] => 58
      [1] => 57
      [2] => 56 
    ) 
)

To assign it to a variable simply do:

$variable = $meta["12"];

prints:

print_r($variable);

Array
(
    [termimages] => Array
        (
            [0] => 58
            [1] => 57
            [2] => 56
        )

)

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237817

So you want to get the key of the first member of an array that is the first member of $meta?

$keys = array_keys($meta[0]);
$key = $keys[0];

You've updated your question:

How to get second key from first array?

I.e., in this case, how to retrieve $meta's second key. The technique is exactly the same as above:

$keys = array_keys($meta);
$key = $keys[1];

(And, if and when array dereferencing comes online, this will be able to be shortened to $key = array_keys($meta)[1];, but alas not yet.)

Upvotes: 1

tim
tim

Reputation: 2724

$array[0][12] = 'string';

Or do u wanna extract the array keys?

Upvotes: 1

Related Questions