简体   繁体   English

如何在PHP中为每个循环创建变量并为其分配值?

[英]How to create variables and assign them values within a for each loop in PHP?

I have for each loop which when looping outputs the items held in a basket array. 对于每个循环,我都有一个循环,该循环输出保存在篮子数组中的项目。 This works great, but I want to store a few things per EACH item in session variables so I can use this information elsewhere, I am trying to create session variables inside the foreach loop, but of course the variables need to have different names every time it loops through a different item. 这很好用,但是我想在会话变量中为每个项目存储一些东西,以便可以在其他地方使用此信息,我试图在foreach循环中创建会话变量,但是当然每次变量都需要使用不同的名称它遍历另一个项目。 I have researched how to create variables dynamically, I couldn't figure it out. 我研究了如何动态创建变量,但无法弄清楚。 This is something I have always felt would come in handy, but as it seems difficult I just avoid it, but now I want to know if its possible. 我一直觉得这很方便,但是似乎很难避免,但现在我想知道它是否可行。

Here is my for each loop code, you can see inside the three session variables I want to create for each item in the basket: 这是我的每个循环代码,您可以在篮子中为每个项目创建的三个会话变量内看到:

foreach ($basketarray as $value)
{
    echo "<div id='basketitems'><br/>                                       
    ".$value['name']."<br/>
    ".$value['id']."<br/>
     &pound;".$value['price']."<br/>
     ".$value['size']."<br/>
    Quantity: ".$value['quantity']."<br/><br/>
    <img id='searchimage' src=".$value['picture']." width='210' height='250' /><br/>";

    $_SESSION['Bprodid'] = $value['id'];
    $_SESSION['Bprodquantity'] = $value['quantity'];
    $_SESSION['Bprodprice'] = $value['price'];

echo "<form action='deletefrombasket.php' name='basketdelete$items' id='basketdelete$items' method='POST'>

    <input type='submit' name='".$value['basketid']."' value='Remove' id='basketid' name='basketid'/>

    </form></div>";

$items++;
}
?>
    <div id='basketdetails'>
<?php

echo "<p>items ". number_format($basketitems)."</p>";
echo "<p>Total &pound; ".number_format($baskettotal, 2, '.', ',')."</p>";


if($basketitems && $baskettotal !=0)
{
    echo "<a  href='clear.php'>Empty Basket</a>";
    echo "<a  href='checkout.php'>Checkout</a>";
}

So it is possible to do something like this? 所以可以做这样的事情吗? I was trying to create varaible names by using a counter but I was unsuccessful. 我试图通过使用计数器来创建易变的名称,但未成功。

I would appreciate any advice. 我将不胜感激任何建议。

Thanks 谢谢

Put the values in arrays. 将值放在数组中。 Then use a counter that increments for each loop to sderver as the key for each value. 然后,使用对每个循环递增的计数器来存储作为每个值的键。

$counter = 0;
foreach ($basketarray as $value)
{
    // Code goes here

    $_SESSION['Bprodid'][$counter] = $value['id'];
    $_SESSION['Bprodquantity'][$counter]  = $value['quantity'];
    $_SESSION['Bprodprice']$counter[]  = $value['price'];

    // More code
    $counter++
}

To access each value use a for loop: 要访问每个值,请使用for循环:

$size = count($_SESSION['Bprodid']);
for ($i = 0; $i < $size; $i++)
{
    echo $_SESSION['Bprodid'][$i] . "<br>\n";
    echo $_SESSION['Bprodquantity'][$i] . "<br>\n";
    echo $_SESSION['Bprodprice'][$i] . "<br><br>\n";
}

Your session variable can be a multi-dimensional array, so before the loop you can say: 您的会话变量可以是多维数组,因此在循环之前您可以说:

$_SESSION['my_values'] = array();

And in the loop: 并在循环中:

$_SESSION['my_values'][$value['id']]['Bprodquantity'] = $value['quantity'];
$_SESSION['my_values'][$value['id']]['price'] = $value['price'];
// etc.

Edit: To loop through all your values you can use: 编辑:要遍历所有值,可以使用:

foreach ($_SESSION['my_values'] as $key => $values)
{
  echo $key; // the ID of your product
  echo $values['quantity']; // the quantity
  echo $values['price']; // the price
}

Store you values into an array $products[$items]["prodId"] and so on and after the loop set your session variable to the array, $_SESSION["products"] = $products . 将值存储到数组$products[$items]["prodId"] ,依此类推,然后在循环中将会话变量设置为数组$_SESSION["products"] = $products You can then access the items with $_SESSION["products"][$itemNumber]["prodId"] and so forth. 然后,您可以使用$_SESSION["products"][$itemNumber]["prodId"]等访问这些项目。

You can use serialize() and unserialize() function to save whole array in session: 您可以使用serialize()和unserialize()函数将整个数组保存在会话中:

$_SESSION['sbasketarray'] = serialize($basketarray);

and in other script which need data: 和其他需要数据的脚本中:

if (isset ( $_SESSION['sbasketarray'] )) {
    $basketarray = unserialize  ( $_SESSION['sbasketarray'] );
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM