0

我有一个数组,我加载如下:

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"
4

2 回答 2

1
$string = join("\n", array_map(function($x) {return "For $x[itemId] ==> $x[qty] sample";}, $orderedItems));
于 2013-04-17T19:05:02.173 回答
0
$string = "";
foreach ($orderedItems as $item)
    $string .= "For item{$item['itemId']} ==> {$item['qty']} sample\n";
$string = rtrim($string);
于 2013-04-17T19:01:11.427 回答