PHP数组仅显示json中的最后一个产品

dldeef67  于 2023-01-14  发布在  PHP
关注(0)|答案(1)|浏览(137)

我有一个购物车,一旦placeorder.php被点击,我就设置了一个webhook来发布到discord。
购物车使用会话数组存储购物车信息。
当按下"添加到卡"按钮时,会话数组在product.php页面上设置如下

$_SESSION['cart'] = array($product_id => $quantity);

产品ID是从mysql数据库中提取的。

    • 在购物车中添加了2个项目后,数组如下所示:**
["cart"]=>
  array(2) {
    [1]=> int(5)
    [4]=> int(3)
  }
    • 如果我重复这个数组,它会变成**
foreach ($products as $product){
    echo $product[name];
    echo $products_in_cart[$product['id']];
}

Output: camera5laptop3

$products_in_cart[$product['id'] is simply grabbing the quantity ordered.
    • JSON结构**

我正在尝试使购物车中的每个产品名称都出现在"订购的产品"字段下的新行(\n)上。数量也是如此。
这将是通过foreach,但由于json结构在json_encode()中,我很挣扎。
以下设置目前仅显示了阵列中的最后一个*$产品['name']* 和 $产品_in_cart [$产品['id ']

$msg = json_encode([
    // Message
    "content" => "",
 
    // Username
    //"username" => "",
 
    // Avatar URL.
    // Uncomment to use custom avatar instead of bot's pic
    //"avatar_url" => "",
 
    // text-to-speech
    "tts" => false,
 
    // file_upload
    // "file" => "",
    // Embeds Array
    
    "embeds" => [
        [
            // Title
            "title" => "New Order",
 
            // Embed Type, do not change.
            "type" => "rich",
 
            // Description
            "description" => "New order, go sort it",
 
            // Link in title
            //"url" => "",
 
            // Timestamp, only ISO8601
            "timestamp" => $timestamp,
 
            // Left border color, in HEX
            "color" => hexdec( "3366ff" ),
 
            // Footer text
            "footer" => [
                "text" => "Sent from thestash.store",
                "icon_url" => ""
            ],
 
            // Embed image
            "image" => [
                "url" => ""
            ],
 
            // thumbnail
            //"thumbnail" => [
            //    "url" => ""
            //],
 
            // Author name & url
            "author" => [
                "name" => "TEST3",
                "url" => ""
            ],
 
            // Custom fields

            "fields" => [
                // Field 1
                
                [
                    "name" => "Ordered Products",
                    "value" => $product['name'],
                    "inline" => true
                ],
                // Field 2
                 // Field 1
                 [
                    "name" => "Quantity",
                    "value" => $products_in_cart[$product['id']],
                    "inline" => true
                ],
                [
                    "name" => "Order Total $",
                    "value" => $subtotal,
                ],
                [
                    "name" => "Customer Alias",
                    "value" => $_SESSION['alias'],
                    "inline" => true
                ],
                [
                    "name" => "Customer Number",
                    "value" => $_SESSION['number'],
                    "inline" => true
                ],
                [
                    "name" => "Affiliation",
                    "value" => $_SESSION['affiliation'],
                    "inline" => true
                ],

                // etc
            ]
        ]
    ]

], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

不协调时的电流输出:

正在尝试输出为(在JSON中设置manuall)

    • 打印_r($消息);**
{
    "content": "",
    "tts": false,
    "embeds": [
        {
            "title": "New Order",
            "type": "rich",
            "description": "New order, go sort it",
            "timestamp": "2023-01-13T11:57:22+00:00",
            "color": 3368703,
            "footer": {
                "text": "Sent from thestash.store",
                "icon_url": ""
            },
            "image": {
                "url": ""
            },
            "author": {
                "name": "TEST3",
                "url": "https://thestash.store"
            },
            "fields": [
                {
                    "name": "Ordered Products",
                    "value": "laptop",
                    "inline": true
                },
                {
                    "name": "Quantity",
                    "value": 3,
                    "inline": true
                },
                {
                    "name": "Order Total $",
                    "value": 96000
                },
                {
                    "name": "Customer Alias",
                    "value": "quigg",
                    "inline": true
                },
                {
                    "name": "Customer Number",
                    "value": "1239871237378127",
                    "inline": true
                },
                {
                    "name": "Affiliation",
                    "value": "yes",
                    "inline": true
                }
            ]
        }
    ]
}

正在尝试使用如下JSON格式。其中,每个$product [name]和$products_in_cart [$product ['id ']]都在一个新行上。当前显示最后一个$product [name]和$products_in_cart [$product ['id']]

{
    "content": "",
    "tts": false,
    "embeds": [
        {
            "title": "New Order",
            "type": "rich",
            "description": "New order, go sort it",
            "timestamp": "2023-01-13T11:57:22+00:00",
            "color": 3368703,
            "footer": {
                "text": "Sent from thestash.store",
                "icon_url": ""
            },
            "image": {
                "url": ""
            },
            "author": {
                "name": "TEST3",
                "url": "https://thestash.store"
            },
            "fields": [
                {
                    "name": "Ordered Products",
                    "value": "camera\nlaptop",
                    "inline": true
                },
                {
                    "name": "Quantity",
                    "value": "5\n3",
                    "inline": true
                },
                {
                    "name": "Order Total $",
                    "value": 96000
                },
                {
                    "name": "Customer Alias",
                    "value": "quigg",
                    "inline": true
                },
                {
                    "name": "Customer Number",
                    "value": "1239871237378127",
                    "inline": true
                },
                {
                    "name": "Affiliation",
                    "value": "yes",
                    "inline": true
                }
            ]
        }
    ]
}
ztyzrc3y

ztyzrc3y1#

所以这只是一个在两个字符串中添加额外内容的例子,用换行符分隔,所以只要循环遍历products数组来创建两个字符串,一个是名称,一个是数量,然后把它们放入变量中,然后你就可以在Fields数组中正确的位置使用这些变量了。
以下是总体思路:

$names = "";
$quantities = "";

foreach ($products as $product){
    $names .= ($names != "" ? "\n" : "").$product["name"];
    $quantities .= ($quantities != "" ? "\n" : "").$products_in_cart[$product['id']];
}

然后在构建数组的位中:

...
"fields" => [
                [
                    "name" => "Ordered Products",
                    "value" => $names,
                    "inline" => true
                ],
                [
                    "name" => "Quantity",
                    "value" => $quantities,
                    "inline" => true
                ],
...

相关问题