1

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!

3
  • 2
    What you have posted is not a valid array Commented Jan 20, 2012 at 21:50
  • as you can see - there is nothing assigned to 0. And it is what I'm talking about Commented Jan 20, 2012 at 21:53
  • Yes indeed.:/ How to get second key from first array? Commented Jan 20, 2012 at 21:56

3 Answers 3

1

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

Or do u wanna extract the array keys?

Sign up to request clarification or add additional context in comments.

2 Comments

Seems like more key not value. Pardon my lack of knowledge about arrays.
In a loop go: foreach ($array as $key => $value)
1

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.)

1 Comment

This one looks like is working $keys = array_keys($meta); $key = $keys[1];
0

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
        )

)

1 Comment

Thank you every one for helping me! I found another solution not related to array.

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.