0

I have an array like:

[meta] => Array (
  [company] => Company, LLC
  [confirmation] => 6391381
  [reference] => None
  [service] => Service
  [timestamp] => 2016-04-25 11:12:54
  [user] => company
)
[result] => Array (
  [action] => REVIEW
  [detail] => TRANSACTION REQUIRES FURTHER ATTENTION
  [issues] => Array (
      [0] => DOB CHECK FAILED
    )
)
[output] =>  Array ( )  

I am attempting to echo the 'action' value like this:

$json_result = json_decode($result, true); 
echo "$json_result[result]['action']";

But instead of getting 'REVIEW' I am getting: 'Array['action']'

Any thoughts?

8
  • 4
    echo $json_result['result']['action'];? Commented Apr 25, 2016 at 15:19
  • @jon when i try it like this i get the following error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) Commented Apr 25, 2016 at 15:23
  • 2
    The code example I gave would not produce that error, or any error assuming your question is accurate. You've used it wrong. Commented Apr 25, 2016 at 15:24
  • Yes it would (if still in double quotes). The single quoted array key inside the double quoted string is wrong. php.net/manual/en/… To use quoted keys inside double quotes, it must be {} enclosed. Commented Apr 25, 2016 at 15:27
  • @MichaelBerkowski Do you see any double quotes in my comment? Commented Apr 25, 2016 at 15:28

3 Answers 3

2

using arrays inside strings leads to madness. or at least horrible frustration.

as Jon Stirling pointed out, in your case why even bother putting the variable in a double quote?

echo $json_result['result']['action'];

works just fine. If you must use an array inside a string, escape it with curly braces

echo "{$json_result['result']['action']}";
Sign up to request clarification or add additional context in comments.

Comments

1

You're missing the apostrophes from the first index:

$json_result[result]['action'];

It should look like this:

$json_result['result']['action'];
             ^      ^

Edit: You can use regular php syntax to address array values if you put the whole expression between curly braces ( { ):

echo "This is the result: {$json_result['result']['action']};"

...or simply remove the double quotes (") from echo.

More info here: php echo

Comments

0

To review all array

print_r($json_result);

To view only action

echo $json_result['result']['action'];

2 Comments

When i try it like this I get the following error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
@TomCanfarotta If you're getting the T_ENCAPSED AND WHITESPACE is't sbecause you still have double quotes surrounding the whole thing as echo "$json_result['result']['action']"; Note the example above does not have them.

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.