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?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Tom Canfarotta
  • 743
  • 1
  • 5
  • 14
  • 4
    `echo $json_result['result']['action'];`? – Jonnix Apr 25 '16 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) – Tom Canfarotta Apr 25 '16 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. – Jonnix Apr 25 '16 at 15:24
  • Yes it would (if still in double quotes). The single quoted array key inside the double quoted string is wrong. http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double To use quoted keys inside double quotes, it must be `{}` enclosed. – Michael Berkowski Apr 25 '16 at 15:27
  • @MichaelBerkowski Do you see any double quotes in my comment? – Jonnix Apr 25 '16 at 15:28
  • @JonStirling No, but I was referring to your second comment ---ohhh I get the context now. The reported error was based on your unquoted example, not the original. – Michael Berkowski Apr 25 '16 at 15:29
  • Here's a full explanation of the quoting rules: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/13935532#13935532 – Michael Berkowski Apr 25 '16 at 15:30
  • Okay, I got it! Remove the double quotes (") from echo. – Gergely Lukacsy Apr 25 '16 at 15:37

3 Answers3

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']}";
serakfalcon
  • 3,501
  • 1
  • 22
  • 33
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

Gergely Lukacsy
  • 2,881
  • 2
  • 23
  • 28
0

To review all array

print_r($json_result);

To view only action

echo $json_result['result']['action'];
WhSol
  • 116
  • 3
  • 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) – Tom Canfarotta Apr 25 '16 at 15:23
  • @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. – Michael Berkowski Apr 25 '16 at 15:31