Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Json格式变量声明_Php_Json - Fatal编程技术网

PHP Json格式变量声明

PHP Json格式变量声明,php,json,Php,Json,我正在使用paypal api获取收款人信息。 我正在得到json结果。如何获取电子邮件,名字,姓氏不同变量 以下是JSON结果: { id: "PAY-4L2624428H450980CLD23F4A", intent: "sale", state: "approved", cart: "74345738MA858411Y", payer: { payment_method: "paypal", status: "VERI

我正在使用paypal api获取收款人信息。
我正在得到json结果。如何获取
电子邮件
名字
姓氏
不同变量

以下是JSON结果:

{
    id: "PAY-4L2624428H450980CLD23F4A",
    intent: "sale",
    state: "approved",
    cart: "74345738MA858411Y",
    payer: {
        payment_method: "paypal",
        status: "VERIFIED",
        payer_info: {
            email: "haj.mohamed-facilitator@pmgasia.com",
            first_name: "test",
            last_name: "facilitator",
            payer_id: "Z2ZSX2WM9ALD2",
            shipping_address: {
                recipient_name: "test facilitator"
            },
            country_code: "SG"
        }
    }
}

您必须将json字符串解码为对象或数组。如果结果是
$json\u str
变量

$result_arr= json_decode($json_str, true) // returns in array
$result_obj= json_decode($json_str) // returns in object

有关更多详细信息,请使用json_decode并使用foreach循环,您将获得包含键和值的所有数据

  $json = "{id: "PAY-4L2624428H450980CLD23F4A",intent: "sale",state: "approved",cart: "74345738MA858411Y",
   payer: {payment_method: "paypal",status: "VERIFIED",payer_info: {email: "haj.mohamed-facilitator@pmgasia.com",first_name: 
   "test",last_name: "facilitator",payer_id: "Z2ZSX2WM9ALD2",shipping_address: {recipient_name: "test facilitator"},country_code: "SG"}}";

 $temp = json_encode($json);

 foreach ($temp as $key=>$value) 
 {
 // $key and  $value
 }

@Haj Mohamed A首先,您的json是无效的,因为键id、意图等没有双引号,因此作为php透视图,此json是无效的,如果您执行
json\u decode($json\u str,true)
,那么您将获得
null
值,因此您必须按照以下方式排列此json:

<?php
  $json_string = '{
                   "id":"PAY-4L2624428H450980CLD23F4A",
                   "intent":"sale",
                   "state":"approved",
                   "cart":"74345738MA858411Y",
                   "payer":{
                      "payment_method":"paypal",
                      "status":"VERIFIED",
                      "payer_info":{
                         "email":"haj.mohamed-facilitator@pmgasia.com",
                         "first_name":"test",
                         "last_name":"facilitator",
                         "payer_id":"Z2ZSX2WM9ALD2",
                         "shipping_address":{
                            "recipient_name":"test facilitator"
                         },
                         "country_code":"SG"
                      }
                   }
                }';
  $infoArr = json_decode($json_string, true);

  //1.Now you can use foreach():

                    //or 2.you can directly get the value by array index like below;
 echo "email : ".$infoArr["payer"]["payer_info"]["email"]."<br>";
 echo "first_name : ".$infoArr["payer"]["payer_info"]["first_name"]."<br>";
 echo "last_name : ".$infoArr["payer"]["payer_info"]["last_name"]."<br>";

如果使用Google,您会发现PHP函数,如果第二个参数设置为
true
,该函数会将JSON作为关联数组返回。给定的JSON不是有效的JSON,请检查[]