简体   繁体   English

如何获取 PHP 单个数组值

[英]How get PHP single Array Value

How I can get only the value "RGE_NOMBRE" for fill an imput text in PHP from the following array:如何从以下数组中仅获取值“RGE_NOMBRE”以填充 PHP 中的输入文本:

object(stdClass)#1367 (1) 
{
 ["GetContribuyentesResult"]=> string(161) "{"RGE_RUC":"102318239",**"RGE_NOMBRE"**:"P ARISTIDES HERNANDEZ C POR A","NOMBRE_COMERCIAL":"P ARISTIDES HERNANDEZ","CATEGORIA":"0","REGIMEN_PAGOS":"2","ESTATUS":"2"}" 
}

Assuming that your response is this:假设您的回复是这样的:

{"RGE_RUC":"102318239","RGE_NOMBRE":"P ARISTIDES HERNANDEZ C POR A","NOMBRE_COMERCIAL":"P ARISTIDES HERNANDEZ","CATEGORIA":"0","REGIMEN_PAGOS":"2","ESTATUS":"2"}

what you have is a JSON object.您所拥有的是 JSON object。 To get what you want, decode the JSON and extract your data from the resulting object:为了得到你想要的,解码 JSON 并从生成的 object 中提取数据:

$jsonObject = json_decode($response);
$requiredData = $jsonObject->RGE_NOMBRE; ///  P ARISTIDES HERNANDEZ C POR A

While your question is not very clear on how you get the string as a value of stdClass object with key GetContribuyentesResult , I am guess you are inserting the key later.虽然您的问题不是很清楚您如何将字符串作为 stdClass object 的值与键GetContribuyentesResult ,但我猜您稍后会插入该键。

As per your comments you are doing $data= (array) $response;根据您的评论,您正在执行$data= (array) $response; and your result is a string.你的结果是一个字符串。 Again this is not possible because you are trying to cast an array.同样,这是不可能的,因为您正在尝试强制转换数组。 Hence I assume $response is the string you are talking about.因此,我假设$response是您正在谈论的字符串。

This string is a JSON string about which you can read more on this page .该字符串是JSON字符串,您可以在此页面上阅读更多信息。

And you can convert a json string to an array with json_decode() in php.您可以使用 php 中的json_decode()将 json 字符串转换为数组。 This is how you can extract any key from a json string.这是您可以从 json 字符串中提取任何密钥的方法。

$response = '{"RGE_RUC":"102318239","RGE_NOMBRE":"P ARISTIDES HERNANDEZ C POR A","NOMBRE_COMERCIAL":"P ARISTIDES HERNANDEZ","CATEGORIA":"0","REGIMEN_PAGOS":"2","ESTATUS":"2"}';

# decoding to an associative array
$array = json_decode($response, true); ## or json_decode($response) to get an stdClass object
$requiredValue = $array['RGE_NOMBRE']; ## or $requiredValue = $array->RGE_NOMBRE; for stdClass object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM