php 如何将 foreach 循环值放入数组

php how to put foreach loop value in array

我想将 foreach 放入数组中的值中,以将值分配为数组中的循环。怎么做?我尝试将 foreach 放入数组中,但看起来是错误的。

我的数组代码:

$allcart[] = $_SESSION["shopping_cart"];

如果我打印我会得到这个值

Array ( [0] => Array ( [item_varid] => 109 [item_id] => 146 [item_basename] => aaa [item_baselink] => products.php?id=146 [item_name] => grey [item_price] => 10 [item_image] => Upload20220224569751018.jpg [item_totalquan] => 120 [item_quantity] => 2 ) [1] => Array ( [item_varid] => 110 [item_id] => 146 [item_basename] => aaa [item_baselink] => products.php?id=146 [item_name] => pink [item_price] => 20 [item_image] => Upload20220224569794304.png [item_totalquan] => 130 [item_quantity] => 4 ) [2] => Array ( [item_varid] => 111 [item_id] => 146 [item_basename] => aaa [item_baselink] => products.php?id=146 [item_name] => yellow [item_price] => 30 [item_image] => Upload20220224731849169.jpg [item_totalquan] => 230 [item_quantity] => 5 ) )

我想把这个$allcart数组放在这个里面

function payPal(array $allcart,$gateway){

try {
            
            $response = $gateway->purchase(array(
                'amount' => $totalprice,
                //
            'items' => array(
                    array(
                        //put below here, replace 'name' value with array value [item_basename] from $allcart
                        'name' => 'Course Subscription',
                        'price' => $_POST['amount'],
                        'description' => 'Get access to premium courses.',
                        'quantity' => 1
                    ),
                ),
                //
                'currency' => PAYPAL_CURRENCY,
                'returnUrl' => PAYPAL_RETURN_URL,
                'cancelUrl' => PAYPAL_CANCEL_URL,
                


            ))->send();
}
function payPal(array $allcart,$gateway){

try {
            foreach ($allcart as $key => $value) {
                    $items[] = array('name' => $value['item_basename'],
                              'price' => $value['item_price'],
                              'description' => 'Get access to premium courses.',
                              'quantity' => $value['item_quantity'] 
               );
               }
            
            $response = $gateway->purchase(array(
                'amount' => $totalprice,
                'items' => $items,
                'currency' => PAYPAL_CURRENCY,
                'returnUrl' => PAYPAL_RETURN_URL,
                'cancelUrl' => PAYPAL_CANCEL_URL,
            ))->send();
}

我希望这会解决您的查询,因为我将您所有的 $allcart 数组存储到新的 $items 数组中并调用了。

编写一个 PHP 程序,将您的商品价格存储在一个数组中,并显示商品名称和价格。 (使用 foreach 循环)