【问题标题】:use "for loop" in array and create multiple variables to array在数组中使用“for循环”并为数组创建多个变量
【发布时间】:2020-09-02 07:21:53
【问题描述】:

我有多个变量,我想将它们移动到数组中

我试试这个,但什么也没发生

定义变量

$product_1_price = (double)$posted_data['post_data']['ad_1kg_kopek'];
$product_2_price = (double)$posted_data['post_data']['ad_415gr_kopek'];
$product_3_price  = (double)$posted_data['post_data']['ad_1kg_kedi'];
$product_4_price  = (double)$posted_data['post_data']['ad_415gr_kedi'];

    $product_1_quantity = (int)$posted_data['post_data']['ad_1kg_kopek_adet'];
    $product_2_quantity = (int)$posted_data['post_data']['ad_415gr_kopek_adet'];
    $product_3_quantity = (int)$posted_data['post_data']['ad_1kg_kedi_adet'];
    $product_4_quantity = (int)$posted_data['post_data']['ad_415gr_kedi_adet'];

if($product_1_price!='0') {
                    $product_1 = "1 Kg Köpek Kuru Mama";
                }

                if($product_2_price!='0') {
                    $product_2 = "1 Kutu (415 gr) Köpek Konserve Mama"; 
                }

                if($product_3_price!='0') {
                    $product_3 = "1 Kg Kedi Kuru Mama"; 
                }

                if($product_4_price!='0') {
                    $product_4 = "1 Kutu (415 gr) Kedi Konserve Mama";  
                }

我使用 givewp 插件,我创建了这个变量,通常它们在数据库中没有。这个变量只保存在订单中

数组代码

$user_basket = array();

                $productvariable['productname'] = array($product_1,$product_2,$product_3,$product_4);
                $productvariable['productprice'] = array($product_1_price*$product_1_quantity,$product_2_price*$product_2_quantity,$product_3_price*$product_3_quantity,$product_4_price*$product_4_quantity);
                $productvariable['productquantity'] = array($product_1_quantity,$product_2_quantity,$product_3_quantity,$product_4_quantity);

                for ($i = 1; $i <= 4; $i++) 
                    {
                    $productname = $productvariable["productname"][$i];
                    $productprice = $productvariable["productprice"][$i];
                    $productquantity = $productvariable["productquantity"][$i];
                    }   

                $user_basket[] = array(

                    $productname, $productprice, $productquantity,

                );

和结果

[[[null,null,null]]]

【问题讨论】:

  • 这样定义 $product_1 = "1 Kg Köpek Kuru Mama";

标签: php arrays wordpress for-loop


【解决方案1】:

我想我解决了我的问题

这没必要

   for ($i = 1; $i <= 4; $i++) 
                    {
                    $productname = $productvariable["productname"][$i];
                    $productprice = $productvariable["productprice"][$i];
                    $productquantity = $productvariable["productquantity"][$i];
                    }   

并改变它;

$user_basket[] = array(

                    $productvariable['productname'], $productvariable['productprice'], $productvariable["productquantity"],

                );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2014-10-25
    相关资源
    最近更新 更多