dongmai6666 2014-03-30 01:00
浏览 19
已采纳

如何遍历这个PHP数组

I am retrieving information from the GetResponse Email Marketing API and my code returns this data from the array:

array(2) {
  ["zAuW"]=>
  object(stdClass)#332 (7) {
    ["optin"]=>
    string(6) "double"
    ["from_email"]=>
    string(17) "email@gmail.com"
    ["name"]=>
    string(23) "asw_getresponse_edition"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "email@gmail.com"
    ["created_on"]=>
    string(19) "2014-03-29 07:27:46"
    ["from_name"]=>
    string(11) "1213456@"
  }
  ["z1Bi"]=>
  object(stdClass)#333 (7) {
    ["optin"]=>
    string(6) "double"
    ["from_email"]=>
    string(17) "email@gmail.com"
    ["name"]=>
    string(7) "test"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "email@gmail.com"
    ["created_on"]=>
    string(19) "2014-03-29 02:54:51"
    ["from_name"]=>
    string(11) "123456@"
  }
}
object(stdClass)#330 (1) {
  ["zAuW"]=>
  object(stdClass)#334 (7) {
    ["optin"]=>
    string(6) "double"
    ["from_email"]=>
    string(17) "email@gmail.com"
    ["name"]=>
    string(23) "asw_getresponse_edition"
    ["description"]=>
    NULL
    ["reply_to_email"]=>
    string(17) "email@gmail.com"
    ["created_on"]=>
    string(19) "2014-03-29 07:27:46"
    ["from_name"]=>
    string(11) "123456@"
  }
}

Here is the code used to get this data:

require_once('GetResponseAPI.class.php');
$api = new GetResponse('YOUR_API_KEY');

// Account
$details = $api->getAccountInfo();
//var_dump($details);

// Campaigns
$campaigns = (array)$api->getCampaigns();
$campaignIDs = array_keys($campaigns);
$campaign = $api->getCampaignByID($campaignIDs[0]);
var_dump($campaigns, $campaign);

I would like to know what PHP code could I use to loop through this array and display the information in a dropdown list. I have tried this but it is oviously wrong:

$output .= '<select class="asw_select'.$field_class.'" name="'.$this->prefix.'_options['.$id.']" id="'.$this->prefix.'_options['.$id.']">';
foreach($campaign as $key => $value)
    $output .= '<option '.selected($current_mailing_list, $value['id'], false).' id="'.esc_attr($value['id']).'" value="'.$value['id'].'">'. __($value['name'], $this->prefix).'</option>';
$output .= '</select>';

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douyuan1752 2014-03-30 02:11
    关注

    You have an array of objects in your response. So, in your foreach loop you should access it's elements like $value->name (not as $value['name'])

    You do not have id attribute for your values. So after above correction, $value->id will be empty. However the $key seems like a unique identifier for me. If this is the case, within your foreach loop you can replace $value->id with $key

    $output.= '<select class="asw_select'.$field_class.'" name="'.$this->prefix.'_options['.$id.']" id="'.$this->prefix.'_options['.$id.']">';
    foreach($campaign as $key => $value)
        $output .= '<option '.selected($current_mailing_list, $key, false).' id="'.esc_attr($key).'" value="'.$key.'">'. __($value->name, $this->prefix).'</option>';
    $output .= '</select>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)