我在函数中有一个foreach,我想返回所有值,然后将其存储在一个变量中,以添加到wp\\u insert\\u post函数中的post\\u内容中。
我尝试回显数组中的所有键并返回,但当我使用return时,它只返回第一个值。
我做了一些研究,人们说我需要将其存储在一个数组中,以便能够返回它,但当我将其添加到帖子内容时,它会回显数组();
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
));
有人能帮忙吗?
最合适的回答,由SO网友:Misha Rudrastyh 整理而成
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
));