3

I get this object in echo $output:

{
  "Key":"a-string-with-letters-and-numbers"
}

How can I store the string ("a-string-with-letters-and-numbers") as a variable, or can i echo this directly with selectors?

I need to store the string into this script:

options({
  key: "<?php echo $output ?>"
});
2
  • Show complete code from where you are getting this object Commented Sep 21, 2015 at 10:35
  • Something like this? $output= $yourObjectName->Key; Commented Sep 21, 2015 at 10:38

4 Answers 4

2

Use json_decode

$output = json_decode('{
  "Key":"a-string-with-letters-and-numbers"
}');

echo $output->Key;
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why Filip not accepts your answer. But i look your the first to answer his question
1

Your object is not an object in PHP, it is a JSON string that you need to decode to convert it in an php object or an array

 $json  = '{"Key":"a-string-with-letters-and-numbers"}';
 $object = json_decode($json);
 echo $object->key; // object

 $array = json_decode($json, true);
 echo $array['key']; // array

Comments

1

you have an object in json format. Suppose you have your object in variable $object.
you can convert the object in to array by $obj_to_arr = json_decode($object, true);
now use the key of object to get its value from array like:
$key_value = $obj_to_arr['key'];

if you don't want to convert your object into array then you can also do it like:
$my_object = json_decode($object); $value = $my_object->key;

Comments

1

You can create dynamic variables in php.

${"required_string"}="required_string". Then access that variable as like normal variable $required_string. But '-' is not allowed in variable.

Hope this will help you.

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.