Php 如何在阵列中存储产品的id

Php 如何在阵列中存储产品的id,php,mysql,mysqli,Php,Mysql,Mysqli,假设我有一个购物车,我通过以下代码从db获取数据: $cart_id = $_GET['cart_id']; $get_add = "SELECT * FROM cart WHERE cart_id = '$cart_id'"; $run_add = mysqli_query($con,$get_add); $cart_items = []; $total_price_to_pay = 0; while ($row_results = mysqli_fetch_array($run_add)){

假设我有一个购物车,我通过以下代码从db获取数据:

$cart_id = $_GET['cart_id'];
$get_add = "SELECT * FROM cart WHERE cart_id = '$cart_id'";
$run_add = mysqli_query($con,$get_add);
$cart_items = [];
$total_price_to_pay = 0;
while ($row_results = mysqli_fetch_array($run_add)){
    $item = array(
        'table_id' => $row_results['table_id'],
        'cart_id' => $row_results['cart_id'],
        'pro_id' => $row_results['product_id'],
        'pro_title' => $row_results['product_title'],
        'pro_price' => $row_results['product_price'],
        'pro_img' => $row_results['product_image'],
        'pro_supplier' => $row_results['product_supplier'],
        'qty' => $row_results['qty'],
        'cart_ip' => $row_results['cart_ip'],
        'pro_total' => $row_results['qty']*$row_results['product_price'],
    );
    $total_price_to_pay +=  $row_results['qty']*$row_results['product_price'];
    $cart_items[] = $item;
}
为了展示结果,我做了以下工作:

    <form method='POST' action=''>
<?php foreach ($cart_items as $cart) {
    echo "<input type='hidden' name='pro_id' value='".$cart['pro_id']."'>
    <h4><a href=''>".$cart['pro_title']."</a></h4>
    <p>".$cart['pro_supplier']."</p>
    <select name='quantites'>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
        <option value='4'>4</option>
        <option value='5'>5</option>
    </select>";
} ?>
    <input name='update' type='submit' value='UPDATE'></input>
</form>


您可以使用
array\u column()
函数获取二维数组中的列数组:

$cart_ids = array_column($cart_items, 'pro_id');

您可以通过每次使用
[]
like-
将pro_id存储在隐藏输入的数组中。
name='pro_id[0]
name='product[0][id]
是使用PHP构建表单数据数组的良好符号。sidenote:this
$item=array(…
是多余的。您可以只使用
$row_results
并将
$row_results['pro_total']=$row_results['qty']*$row_results['product_price']
添加到其中。@br.Blue先生无法使用[]在php变量中读取您正在执行的
[/code>$pro id[]
.Html name-
name=“pro id[]”
-是另一回事。
$cart_ids = array_column($cart_items, 'pro_id');