0

i have a foreach within a function where i want to return all the values and then store this in a variable to add to the post_content in a wp_insert_post function.

I've tried echoing all the keys within the array and also returning but when i use return it only returns the first value.

I've done some research and people are saying i need to store this in an array to be able to return it but when i then add this to the post content it echos out array();

function returncartdata($data) {     
    foreach ($data as $carthtml => $itemhtml) {
        echo $itemhtml[0];
        echo $itemhtml[1];
        echo $itemhtml[2];
        echo $itemhtml[3];
        echo $itemhtml[4];
    }               
}
$rawhtmlcartdata = cart_items_array();
$ordercart = returncartdata($rawhtmlcartdata);

wp_insert_post(array (
           'post_type' => 'saved-orders',
           'post_title' => 'Test',
           'post_content' => $ordercart,
           'post_status' => 'publish',
           'comment_status' => 'closed',   // if you prefer
           'ping_status' => 'closed',      // if you prefer
        ));

Can anyone help please?

closed as off-topic by Max Yudin, Rarst, Jacob Peattie, Jack Johansson, cjbj Nov 13 '17 at 14:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This is general PHP question and is off-topic here. – Max Yudin Oct 22 '17 at 14:39
1

Try this:

function returncartdata($data) {
        $return = '';
    foreach ($data as $carthtml => $itemhtml) {
        $return .= $itemhtml[0];
        $return .= $itemhtml[1];
        $return .= $itemhtml[2];
        $return .= $itemhtml[3];
        $return .= $itemhtml[4];
    }           
        return $return;    
}
$rawhtmlcartdata = cart_items_array();
$ordercart = returncartdata($rawhtmlcartdata);

wp_insert_post(array (
           'post_type' => 'saved-orders',
           'post_title' => 'Test',
           'post_content' => $ordercart,
           'post_status' => 'publish',
           'comment_status' => 'closed',   // if you prefer
           'ping_status' => 'closed',      // if you prefer
        ));

Not the answer you're looking for? Browse other questions tagged or ask your own question.