1

another little question: (previous here)

if i have an array that needs a loop?

like this one:

 /*
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] => [email protected]
                    [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
        )

)

how can i get on more lines?

like:

  1. [email protected], 45
  2. [email protected], 45

i love u all guys, u helps me a lot!

2
  • What is your question? How i loop trough a array? Commented Nov 20, 2014 at 15:21
  • edited question: have an output like a list Commented Nov 20, 2014 at 15:24

2 Answers 2

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>';
Sign up to request clarification or add additional context in comments.

4 Comments

Ofcourse it does :p Try reading up on using foreach() at php.net/manual/en/control-structures.foreach.php
and if i want to table that? ex keys as rows and 'reason' and 'detail' as column. :)
a little hint, to format a date from that array? i've tried-> . date_format($subArray['created_at'], 'd/m/y') .
@MicheleMarri $time = new DateTime($subArray['created_at']); echo $time->format('d/m/Y');
-4

array_map function comes to the rescue:

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)
);

2 Comments

help me more please ;) i'm studying this code... $nameofarray - is my array $resultofmapping - is the result to echo where do i have to put these?
This is just bad code. Running an iteration within array_map is totally unnecessary. Not just in this case, but in any case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.