数组结果变量

时间:2013-08-30 13:59:43

标签: php arrays variables

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

5 个答案:

答案 0 :(得分:1)

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

See the reference

在您的示例中,这将是:

$data = json_decode($json_data);

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

使用extract()最简单的PHP Array->变量 像这样:

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

名为client_keyname的变量

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

答案 3 :(得分:0)

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

答案 4 :(得分:-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,我被他的数组关键字诱惑了..如果这不是我想要的,请原谅我。