从php(mandrill)中的多维数组中提取值

时间:2014-11-20 15:19:04

标签: php jquery arrays mandrill

另一个小问题:(上一个here

如果我有一个需要循环的数组?

像这样:

 /*
Array
(
    [0] => Array
        (
            [email] => example email
            [reason] => hard-bounce
            [detail] => 550 mailbox does not exist
            [created_at] => 2013-01-01 15:30:27
            [last_event_at] => 2013-01-01 15:30:27
            [expires_at] => 2013-01-01 15:30:49
            [expired] => 
            [sender] => Array
                (
                    [address] => sender.example@mandrillapp.com
                    [created_at] => 2013-01-01 15:30:27
                    [sent] => 42
                    [hard_bounces] => 45
                    [soft_bounces] => 47
                    [rejects] => 42
                    [complaints] => 42
                    [unsubs] => 42
                    [opens] => 42
                    [clicks] => 42
                    [unique_opens] => 42
                    [unique_clicks] => 42
                )

            [subaccount] => example subaccount
        )

)

我怎样才能获得更多线路?

像:

  1. email@example.com,45
  2. email2 @ example.com,45
  3. 我爱你们所有人,你们帮助了我很多!

2 个答案:

答案 0 :(得分:1)

echo '<table>';
foreach($array as $key => $subArray) {
   echo '<tr><th>' . $key . '</th><td>' . $subArray['email'] . '</td><td>' . $subArray['sender']['hard_bounces'] . '</td><td>' . $subArray['reason'] . '</td><td>' . $subArray['detail'] . '</td></tr>';
}
echo '</table>';

答案 1 :(得分:-4)

array_map功能得救:

implode(', ',               // will prepare a string from array
  array_map(function($el) { // will map existing array to new values
    $result = array();      // prepare result
    foreach(array('email', 'reason') as $name) { // select only email and reason fields 
      $result[$name] = $el[$name];               // setup result
    }
    return $result;         // return to map
  }, $array)
);