0

I have the following:

Array ( [0] => Array ( [0] => Array ( [value] => 150109 [format] => [safe_value] => 150109 ) ) )

I need to get the value "150109", but how on earth do I accomplish that?

2
  • $data[0][0]["value"] or $data[0][0]["safe_value"] if the $data is your array Commented Feb 16, 2017 at 13:27
  • did you try googling for an answer? You should be able to find other answers on accessing multidimensional array values. Commented Feb 16, 2017 at 13:45

2 Answers 2

2

Top level:

print_r($data);
// Output: Array ( [0] => Array ( [0] => Array ( [value] => 150109 [format] => [safe_value] => 150109 ) ) )

Outmost element:

print_r($data[0]);
// Output: Array ( [0] => Array ( [value] => 150109 [format] => [safe_value] => 150109 ) )

Next level:

print_r($data[0][0]);
// Output: Array ( [value] => 150109 [format] => [safe_value] => 150109 )

The final value

echo $data[0][0]['value'];
// Output: 150109

Accessing each layer of values this way makes it easier to figure out how to get to your desired value. After a while this becomes more obvious.

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

5 Comments

Only the first one (top level) will actually output someting. So print_r($data); gives me: Array ( [und] => Array ( [0] => Array ( [value] => 150109 [format] => [safe_value] => 150109 ) ) ). This construction looks a bit different from my initial array.
@oivindr Then you have different data than in your example. However, to get the value in that array, you would do $data['und'][0]['value'].
Thank you! I need to clean up my code, but it looks like it's working. BTW, I got the first array constructin when I used: print_r(array_values($artnr)); Not sure why they differ.
It should not. Please accept my answer if it solved your problem.
@oivindr Please click the v under the up/down-votes to this question to mark it as the correct solution. Indicating that this answer helped you makes it easier for people who may have a similar problem in the future.
0

You can use get values of arrays continusly on multidimensional arrays:
$value = $array[0][0]["value"];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.