【问题标题】:MyEmma API PHP Array and For or While LoopMyEmma API PHP 数组和 For 或 While 循环
【发布时间】:2017-05-15 05:51:10
【问题描述】:

我正在使用 MyEmma API。 MyEmma 是一款基于云的电子邮件营销软件。尽管如果您没有听说过或使用过它,您仍然可以为我的问题提供一些帮助,因为它主要处理数据数组并从 For 或 While PHP 循环中提取值。

所以我有以下代码,将在最后使用 print_r($head) 输出 $head 变量,正如您在代码底部看到的那样:

    <?php
// Authentication Variables
$account_id = "1788878";
$public_api_key = "ccXXXXXXXXXXXXX3";
$private_api_key = "cXXXXXXXXXXXXX5";

// Set URL
$url =   "https://api.e2ma.net/" . $account_id . "/members";  

// Open connection
$ch = curl_init();

// Make the curl call
curl_setopt($ch, CURLOPT_USERPWD, $public_api_key . ":" . $private_api_key);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);



// check for errors
if($http_code > 200) {
    print "Error getting member:\r\n";
    print_r($head);
} else {
    print "Member information:\r\n";
    print_r($head);
}
?>

以下输出是 print_r($head) 生成的示例:

 [ { "status": "active", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158888, "last_modified_at": null, "member_status_id": "a", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:09:31", "bounce_count": 0, "deleted_at": null, "email": "david@something.com" }, { "status": "error", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158889, "last_modified_at": "@D:2014-06-09T09:00:19", "member_status_id": "e", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:10:15", "bounce_count": 1, "deleted_at": null, "email": "test@something.com" } ]

在上面的输出示例中,这些只是脚本实际输出的数百个实体中的两个。

我想要完成的是在 PHP 中使用“for”或“while”循环或类似的东西来获取每个“member_id”值和“email”值,并将在 SQL INSERT 语句中使用为每组 member_id 和 email 值创建一个数据库记录。

我需要帮助的是创建循环以提取我将在数据库表中创建的每条记录的两个值的语法。有人可以告诉我如何正确编写代码来做到这一点吗?

【问题讨论】:

  • 这看起来像正确的 json,所以 json_decode 它并循环遍历你得到的数组!
  • $data=json_decode($head,1) - 然后是foreach($data as $item) ... echo $item['member_id'];

标签: php arrays loops variables for-loop


【解决方案1】:

您将获得正确的json 作为回报。所以你可以decode它并将其用作数组。

<?php

$head = '[ { "status": "active", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158888, "last_modified_at": null, "member_status_id": "a", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:09:31", "bounce_count": 0, "deleted_at": null, "email": "david@something.com" }, { "status": "error", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158889, "last_modified_at": "@D:2014-06-09T09:00:19", "member_status_id": "e", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:10:15", "bounce_count": 1, "deleted_at": null, "email": "test@something.com" } ]';

$data = json_decode($head,true); // the second param will cast it as array, default is obejct. See the linked docs of json_decode!
foreach($data as $item) {
    $id=$item['member_id'];
    $email=$item['email'];
    echo "ID: $id EMAIL: $email<br/>";
    // put your code here to write into DB or whatever you wanna do!
}

?>

替代方案:将其用作对象:

<?php
$data = json_decode($head); // defaults to object if second param isn't set.
foreach($data as $item) {
   $id=$item->member_id;
   $email=$item->email;
   echo "ID: $id EMAIL: $email<br/>";
   // put your code here to write into DB or whatever you wanna do!
}
?>

【讨论】:

  • 完美!谢谢杰夫......这有效。还有一个问题......如果我想从数组中获取名字和姓氏,如果json返回如下所示,我将如何编写变量:{“status”:“active”,“confirmed_opt_in”:null, “account_id”:1738888,“fields”:{“first_name”:“Ash”,“last_name”:“Wright”},“member_id”:262268888,“last_modified_at”:“@D:2016-12-30T19:58: 25", "member_status_id": "a", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-05-05T16:01:21", "bounce_count": 0, "deleted_at ": null, "email": "ashwright@gmail.com" }
  • 对于 member_id 和 email 它是一样的(它只是没有包装在一个数组中,所以你不需要 foreach 循环)。对于last_name,它是$item-&gt;fields-&gt;last_name$item['fields']['last_name']
猜你喜欢
  • 2014-01-16
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 2013-06-12
相关资源
最近更新 更多