我有一个数组,我加载如下:
foreach ($_SESSION['cart'] as $item)
{
$pid = $item['itemId'];
$q = $item['qty'];
$orderedItems[]=array('itemId'=>$pid,'qty'=>$q);
}
我现在需要创建一个变量,它将数组的结果存储在一个字符串中,如下所示:
"For itemA ==> 2 sample
For itemB ==> 1 sample"
$string = join("\n", array_map(function($x) {return "For $x[itemId] ==> $x[qty] sample";}, $orderedItems));
$string = "";
foreach ($orderedItems as $item)
$string .= "For item{$item['itemId']} ==> {$item['qty']} sample\n";
$string = rtrim($string);