如何将对象中的字符串存储为变量?
How can I store string from object as a variable?
我在 echo $output:
中得到这个对象
{
"Key":"a-string-with-letters-and-numbers"
}
如何将字符串 ("a-string-with-letters-and-numbers") 存储为变量,或者我能否直接用选择器回显它?
我需要将字符串存储到这个脚本中:
options({
key: "<?php echo $output ?>"
});
使用json_decode
$output = json_decode('{
"Key":"a-string-with-letters-and-numbers"
}');
echo $output->Key;
您的对象不是 PHP 中的对象,它是一个 JSON 字符串,您需要对其进行解码以将其转换为 php 对象或数组
$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
您有一个 json 格式的对象。假设您的对象在变量 $object
.
中
您可以通过 $obj_to_arr = json_decode($object, true);
将对象转换为数组
现在使用对象的 key
从数组中获取其值,例如:
$key_value = $obj_to_arr['key'];
如果您不想将对象转换为数组,那么您也可以这样做:
$my_object = json_decode($object);
$value = $my_object->key;
您可以在 php 中创建动态变量。
${"required_string"}="required_string"。
然后像普通变量 $required_string 一样访问该变量。
但是 '-' 不允许出现在变量中。
希望对您有所帮助。
我在 echo $output:
中得到这个对象{
"Key":"a-string-with-letters-and-numbers"
}
如何将字符串 ("a-string-with-letters-and-numbers") 存储为变量,或者我能否直接用选择器回显它?
我需要将字符串存储到这个脚本中:
options({
key: "<?php echo $output ?>"
});
使用json_decode
$output = json_decode('{
"Key":"a-string-with-letters-and-numbers"
}');
echo $output->Key;
您的对象不是 PHP 中的对象,它是一个 JSON 字符串,您需要对其进行解码以将其转换为 php 对象或数组
$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
您有一个 json 格式的对象。假设您的对象在变量 $object
.
中
您可以通过 $obj_to_arr = json_decode($object, true);
将对象转换为数组
现在使用对象的 key
从数组中获取其值,例如:
$key_value = $obj_to_arr['key'];
如果您不想将对象转换为数组,那么您也可以这样做:
$my_object = json_decode($object);
$value = $my_object->key;
您可以在 php 中创建动态变量。
${"required_string"}="required_string"。 然后像普通变量 $required_string 一样访问该变量。 但是 '-' 不允许出现在变量中。
希望对您有所帮助。