php mail() and arrays

Discussion in 'PHP' started by liam1412, Oct 20, 2007.

  1. #1
    Hi

    I don't really use arrays a great deal and perhaps I should

    I have an array and I need to get the contents of it echoed into an e-mail

    can anyone help

    Thanks
     
    liam1412, Oct 20, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $body = implode("\n", $array);
    
    PHP:
    If that's not good.... what does your array look like?

    If you have significant array keys, perhaps this would be better.
    
    $body = '';
    
    foreach ($array AS $key => $value)
    {
        $body .= "{$key}: {$value}\n";
    }
    
    PHP:
     
    nico_swd, Oct 20, 2007 IP
  3. liam1412

    liam1412 Active Member

    Messages:
    387
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I have used the second option but is saying the following

    warning: Invalid argument supplied for foreach() in /home/kobekatt/public_html/smoothhomeloans/qualifier.php on line 418
     
    liam1412, Oct 20, 2007 IP
  4. rspenc29

    rspenc29 Peon

    Messages:
    256
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    make sure to change $array to your actual array
     
    rspenc29, Oct 20, 2007 IP
  5. liam1412

    liam1412 Active Member

    Messages:
    387
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Yeah I have already done that but cheers for the heads up mate.

    Thanks
     
    liam1412, Oct 20, 2007 IP
  6. rspenc29

    rspenc29 Peon

    Messages:
    256
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    did you try print_r($array); before the foreach loop to make sure it had valid data?
     
    rspenc29, Oct 20, 2007 IP
  7. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    an easy way to get the contents of an array mailed to you:

    mail('you@domain.tld', 'subject', print_r($array, 1), 'From: ');

    print_r($array, 1) will return the contents of the array instead of printing/echoing it like print_r($array) does.

    -the mole
     
    themole, Oct 20, 2007 IP