0

我有一个php脚本,结果如​​下array

 {
          "success": true,
          "client": {
             "id": "1",
             "email": "jondoe@email.com",
             "password": "474bf122c92de249ace867a003cb7196",
             "lastlogin": "2011-11-25 04:32:40",
             "ip": "213.54.21.3",
             "host": "cmt-random.uk",
             "status": "Active",
             "parent_id": "0",
             "firstname": "John",
             "lastname": "Doe",
             "companyname": "",
             "address1": "Address 54",
             "address2": "",
             "city": "Soullans",
             "state": "Birmingham",
             "postcode": "B33 8TH",
             "country": "GB",
             "phonenumber": "357755733",
             "datecreated": "2011-09-24",
             "notes": "",
             "language": "spanish",
             "company": "0",
             "credit": "0.00",
             "taxexempt": "0",
             "latefeeoveride": "0",
             "cardtype": "Visa",
             "cardnum": null,
             "expdate": null,
             "overideduenotices": "0",
             "client_id": "1",
             "currency_id": "0",
             "countryname": "United Kingdom"
          },
          "call": "getClientDetails",
          "server_time": 1323442995
       }

我的问题是如何将它们放入上面的变量中 $email = $client_email

4

5 回答 5

1

这是一个JSON对象,这意味着您应该能够使用json_decode.

见参考

在您的示例中,这将是:

$data = json_decode($json_data);

如果这需要在客户端使用,则需要将其作为 json 对象读取,以便正确访问它。

于 2013-08-30T14:06:51.850 回答
1

为什么所有这些菜鸟都试图使用“提取”?不要这样做!只是为了阻止这种疯狂:

$data=json_decode($data,true);
$email= $data['client']['email']; 
于 2013-08-30T14:14:41.137 回答
0

PHP Array->variables 最简单的方法是extract()

extract($var_array, EXTR_PREFIX_ALL, "client_"); //all keys in array will be extracted as 

名为 client_keyname 的变量

然而,这看起来不像一个普通的 PHP 数组——所以请确认你的类型!

于 2013-08-30T14:09:36.670 回答
0

解决方案是使用这个 json 数组: $email = $return->client->email;

于 2013-09-09T09:56:40.320 回答
-1
 $array = array (
    "clients" => array (
        "id"      => 1,
        "name"  => "Dont know",
        "email" => "byteme@webyte.com",
 )
);

extract ($array ["clients"]);

echo $email (email is the arrays value);

编辑没有意识到他的数组是 json,我被他的数组关键字诱惑了。如果这不是想要的,请原谅我。

于 2013-08-30T14:12:20.783 回答