数组结果到变量


Array result to variable

我有一个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以上?

为什么所有这些noob都试图使用"extract"?不要这么做!只是为了阻止这种疯狂:

$data=json_decode($data,true);
$email= $data['client']['email']; 

这是一个JSON对象,这意味着您应该能够使用json_decode将其转换为PHP变量。

(参见参考资料)

在您的示例中,这将是:

$data = json_decode($json_data);

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

使用extract()最容易实现PHP数组->变量像这样:

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

名为client_keyname 的变量

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

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

 $array = array (
    "clients" => array (
        "id"      => 1,
        "name"  => "Dont know",
        "email" => "byteme@webyte.com",
 )
);
extract ($array ["clients"]);
echo $email (email is the arrays value);

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