php - how make these variables dynamic in foreach loop -
i have cart data , want use paypal want make cart data formate
code:
$item_1 = new item(); $item_1->setname('item 1') // item name ->setcurrency('usd') ->setquantity(2) ->setprice('15'); // unit price $item_2 = new item(); $item_2->setname('item 2') ->setcurrency('usd') ->setquantity(4) ->setprice('7'); $item_3 = new item(); $item_3->setname('item 3') ->setcurrency('usd') ->setquantity(1) ->setprice('20'); // add item list $item_list = new itemlist(); $item_list->setitems(array($item_1, $item_2, $item_3));
you can define varying data in assoc array first:
$data = array( array( 'quantity' => 1, 'price' => '15'), ... // etc );
then iterate on , add result array:
$result = array(); for($i = 0; $i < count($data); ++$i) { $obj = new item(); $obj->setname('item ' . ($i+1)) // item name ->setcurrency('usd') ->setquantity($data[i]['quantity']) ->setprice($data[i]['price']); $result[] = $obj; } $item_list = new itemlist(); $item_list->setitems($result);
Comments
Post a Comment